Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. adding lables to ListBox

adding lables to ListBox

Scheduled Pinned Locked Moved C#
helpquestionlounge
8 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sabry1905
    wrote on last edited by
    #1

    Hello All i am trying to add label controls into a listbox control in the runtime i am using this code private void button1_Click(object sender, EventArgs e) { Label l = new Label(); l.AutoSize = true; l.Font=listBox1.Font; Random R=new Random(); int r=R.Next(0,255); int g=R.Next(0,255); int b=R.Next(0,255); l.ForeColor = Color.FromArgb(r, g, b); l.TextAlign = ContentAlignment.MiddleCenter; l.Text = "Experement"; listBox1.Items.Add(l); label1.Text = listBox1.Items.Count.ToString(); listBox1.Invalidate(); } i think it have no problems, but i cant see anything added to the listbox items. any help?

    D realJSOPR C L 4 Replies Last reply
    0
    • S Sabry1905

      Hello All i am trying to add label controls into a listbox control in the runtime i am using this code private void button1_Click(object sender, EventArgs e) { Label l = new Label(); l.AutoSize = true; l.Font=listBox1.Font; Random R=new Random(); int r=R.Next(0,255); int g=R.Next(0,255); int b=R.Next(0,255); l.ForeColor = Color.FromArgb(r, g, b); l.TextAlign = ContentAlignment.MiddleCenter; l.Text = "Experement"; listBox1.Items.Add(l); label1.Text = listBox1.Items.Count.ToString(); listBox1.Invalidate(); } i think it have no problems, but i cant see anything added to the listbox items. any help?

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      Why are you adding Labels to a ListBox? What are you trying to achieve with this?? i can't think of any good reason to do this.

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
           2006, 2007, 2008

      1 Reply Last reply
      0
      • S Sabry1905

        Hello All i am trying to add label controls into a listbox control in the runtime i am using this code private void button1_Click(object sender, EventArgs e) { Label l = new Label(); l.AutoSize = true; l.Font=listBox1.Font; Random R=new Random(); int r=R.Next(0,255); int g=R.Next(0,255); int b=R.Next(0,255); l.ForeColor = Color.FromArgb(r, g, b); l.TextAlign = ContentAlignment.MiddleCenter; l.Text = "Experement"; listBox1.Items.Add(l); label1.Text = listBox1.Items.Count.ToString(); listBox1.Invalidate(); } i think it have no problems, but i cant see anything added to the listbox items. any help?

        realJSOPR Offline
        realJSOPR Offline
        realJSOP
        wrote on last edited by
        #3

        You spelled "Experiment" wrong.

        "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
        -----
        "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

        1 Reply Last reply
        0
        • S Sabry1905

          Hello All i am trying to add label controls into a listbox control in the runtime i am using this code private void button1_Click(object sender, EventArgs e) { Label l = new Label(); l.AutoSize = true; l.Font=listBox1.Font; Random R=new Random(); int r=R.Next(0,255); int g=R.Next(0,255); int b=R.Next(0,255); l.ForeColor = Color.FromArgb(r, g, b); l.TextAlign = ContentAlignment.MiddleCenter; l.Text = "Experement"; listBox1.Items.Add(l); label1.Text = listBox1.Items.Count.ToString(); listBox1.Invalidate(); } i think it have no problems, but i cant see anything added to the listbox items. any help?

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          I am surprised that this builds and runs. A list box Items collection is a collection of ListBoxItems, I'd have thought. This is winforms, it's not WPF.

          Christian Graus Driven to the arms of OSX by Vista.

          1 Reply Last reply
          0
          • S Sabry1905

            Hello All i am trying to add label controls into a listbox control in the runtime i am using this code private void button1_Click(object sender, EventArgs e) { Label l = new Label(); l.AutoSize = true; l.Font=listBox1.Font; Random R=new Random(); int r=R.Next(0,255); int g=R.Next(0,255); int b=R.Next(0,255); l.ForeColor = Color.FromArgb(r, g, b); l.TextAlign = ContentAlignment.MiddleCenter; l.Text = "Experement"; listBox1.Items.Add(l); label1.Text = listBox1.Items.Count.ToString(); listBox1.Invalidate(); } i think it have no problems, but i cant see anything added to the listbox items. any help?

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            You wouldn't think it was the season of goodwill, given the responses from the first few people, would you? OK - your problem is that, while ListBox items are a collection of Objects, it has no idea how to display a label - so you get nothing. Your code will show (in label1) that the items are added to the collection - if you want to draw each in a different colour, then you will have to help the listbox by drawing each item yourself. See here[^] for a quick and dirty example - it should give you the idea to work on anyway. And to the other responders - why the -ve attitude, guys? Time to pull some heads out of some arses, I say!

            If I knew then what I know today, then I'd know the same now as I did then - then what would be the point? .\\axxx (That's an 'M')

            D 1 Reply Last reply
            0
            • L Lost User

              You wouldn't think it was the season of goodwill, given the responses from the first few people, would you? OK - your problem is that, while ListBox items are a collection of Objects, it has no idea how to display a label - so you get nothing. Your code will show (in label1) that the items are added to the collection - if you want to draw each in a different colour, then you will have to help the listbox by drawing each item yourself. See here[^] for a quick and dirty example - it should give you the idea to work on anyway. And to the other responders - why the -ve attitude, guys? Time to pull some heads out of some arses, I say!

              If I knew then what I know today, then I'd know the same now as I did then - then what would be the point? .\\axxx (That's an 'M')

              D Offline
              D Offline
              Dave Kreskowiak
              wrote on last edited by
              #6

              Father Christmas wrote:

              And to the other responders - why the -ve attitude, guys? Time to pull some heads out of some arses, I say!

              Because there's just wayyyyy too many people running around trying to do stupid things for the most insiane reasons and we're kind of tired of wasting our time on something that doesn't solve the real problem that the user didn't tell you about. You haven't been around here for the last 5 years watching the quality of questions just plummet to unbelievable levels. I blame it on the outsourcing boom, which generated a huge demand for computer jobs in third world countries where the number of PC's per capita is so low that it could not possibly generate the level of experience required to support the West's hunger for IT personell.

              A guide to posting questions on CodeProject[^]
              Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                   2006, 2007, 2008

              L 1 Reply Last reply
              0
              • D Dave Kreskowiak

                Father Christmas wrote:

                And to the other responders - why the -ve attitude, guys? Time to pull some heads out of some arses, I say!

                Because there's just wayyyyy too many people running around trying to do stupid things for the most insiane reasons and we're kind of tired of wasting our time on something that doesn't solve the real problem that the user didn't tell you about. You haven't been around here for the last 5 years watching the quality of questions just plummet to unbelievable levels. I blame it on the outsourcing boom, which generated a huge demand for computer jobs in third world countries where the number of PC's per capita is so low that it could not possibly generate the level of experience required to support the West's hunger for IT personell.

                A guide to posting questions on CodeProject[^]
                Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                     2006, 2007, 2008

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                OK - I can see your point (and your response, frankly, wasn't the worst) but it was pretty obvious to me tha the OP had a simple conceptual problem - I have a collection of objects in a list box, I am adding labels to it, and I am not seeing anything. Now you said "Why are you adding Labels to a ListBox? What are you trying to achieve with this?? " Q 1 and 2 are good questions - by assumptions may be erroneous and further clarification would be good (in case he doesn't just want colourful items in the listbox, but has some other goal) but your statement "i can't think of any good reason to do this. is, I feel, negative and, frankly, pompous. The OP has, it seems, made a mistake in trying to achieve something (presumably colourful items in a list box) and rather than taking a positive approach, you come over all uppity. And no, I haven't "been around here for the last 5 years watching the quality of questions just plummet to unbelievable levels". However, if this has been the general level of response in some attempt to get a better standard of question - then it plainly isn't working. It's astonishing how politeness can often get better results than snideness. How a positive attitude can elicit the same. Do unto others, and all that. I remember in my programming youth I would have killed for a site like CP. I'd hate to see people not using the site for fear of humiliation.

                If I knew then what I know today, then I'd know the same now as I did then - then what would be the point? .\\axxx (That's an 'M')

                D 1 Reply Last reply
                0
                • L Lost User

                  OK - I can see your point (and your response, frankly, wasn't the worst) but it was pretty obvious to me tha the OP had a simple conceptual problem - I have a collection of objects in a list box, I am adding labels to it, and I am not seeing anything. Now you said "Why are you adding Labels to a ListBox? What are you trying to achieve with this?? " Q 1 and 2 are good questions - by assumptions may be erroneous and further clarification would be good (in case he doesn't just want colourful items in the listbox, but has some other goal) but your statement "i can't think of any good reason to do this. is, I feel, negative and, frankly, pompous. The OP has, it seems, made a mistake in trying to achieve something (presumably colourful items in a list box) and rather than taking a positive approach, you come over all uppity. And no, I haven't "been around here for the last 5 years watching the quality of questions just plummet to unbelievable levels". However, if this has been the general level of response in some attempt to get a better standard of question - then it plainly isn't working. It's astonishing how politeness can often get better results than snideness. How a positive attitude can elicit the same. Do unto others, and all that. I remember in my programming youth I would have killed for a site like CP. I'd hate to see people not using the site for fear of humiliation.

                  If I knew then what I know today, then I'd know the same now as I did then - then what would be the point? .\\axxx (That's an 'M')

                  D Offline
                  D Offline
                  danzar
                  wrote on last edited by
                  #8

                  Father Christmas wrote:

                  I'd hate to see people not using the site for fear of humiliation.

                  Well said!!

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups