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. How can I build a ListBox array?

How can I build a ListBox array?

Scheduled Pinned Locked Moved C#
questiondata-structures
8 Posts 5 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.
  • M Offline
    M Offline
    memix
    wrote on last edited by
    #1

    Hi everybody, I need to constitute a ListBox array (like listBox[i]) , which the user can add various number of listboxes to form in runtime. Is there any way to do that? Thanks... memix

    A R P M 4 Replies Last reply
    0
    • M memix

      Hi everybody, I need to constitute a ListBox array (like listBox[i]) , which the user can add various number of listboxes to form in runtime. Is there any way to do that? Thanks... memix

      A Offline
      A Offline
      Andrei Ungureanu
      wrote on last edited by
      #2

      You simply have to declare something like: ListBox []lbArray = new ListBox[100]; for (int i = 0 ; i < lbArray.length ; i++) { lbArray[i] = new ListBox(); //add the list box to the form } Hope it helps

      Do your best to be the best

      1 Reply Last reply
      0
      • M memix

        Hi everybody, I need to constitute a ListBox array (like listBox[i]) , which the user can add various number of listboxes to form in runtime. Is there any way to do that? Thanks... memix

        R Offline
        R Offline
        Russell Jones
        wrote on last edited by
        #3

        if you're using .net 2.0 i'd be tempted to use Collection instead. That way you don't need to know how many listboxes the user is likely to add. HTH Russ

        1 Reply Last reply
        0
        • M memix

          Hi everybody, I need to constitute a ListBox array (like listBox[i]) , which the user can add various number of listboxes to form in runtime. Is there any way to do that? Thanks... memix

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          What you would normally do, is instantiate your listbox and then add it into the form Controls collection (or relevant child control if hosting in a groupbox or something). To see how this works, take a look at what you get when you add a listbox to a form in Visual Studio.

          Deja View - the feeling that you've seen this post before.

          S 1 Reply Last reply
          0
          • P Pete OHanlon

            What you would normally do, is instantiate your listbox and then add it into the form Controls collection (or relevant child control if hosting in a groupbox or something). To see how this works, take a look at what you get when you add a listbox to a form in Visual Studio.

            Deja View - the feeling that you've seen this post before.

            S Offline
            S Offline
            sujithkumarsl
            wrote on last edited by
            #5

            It will better to use sortedList or hashTable to store the list box with the key as List box name. SortedList listBoxItems = new SortedList(); foreach(ListBox listBox ) { listBoxItems.Add(listBox); }

            My small attempt...

            P 1 Reply Last reply
            0
            • M memix

              Hi everybody, I need to constitute a ListBox array (like listBox[i]) , which the user can add various number of listboxes to form in runtime. Is there any way to do that? Thanks... memix

              M Offline
              M Offline
              memix
              wrote on last edited by
              #6

              Dear friends, Thanks for all of you, I succeed it, it works what I want, at the end my solution is here: public int numberOfListBoxes =0; ListBox[] lbArray; private void button1_Click(object sender, EventArgs e) { numberOfListBoxes = Convert.ToInt32(textBox1.Text); lbArray = new ListBox[numberOfListBoxes]; int i; int listBoxDistances=100; for (i = 0; i < numberOfListBoxes; i++) { lbArray[i] = new ListBox(); this.lbArray[i].Location = new System.Drawing.Point(100+(i*listBoxDistances), 200); this.lbArray[i].Name = "lbArray"+"["+i+"]"; this.lbArray[i].Size = new System.Drawing.Size(57, 100); this.lbArray[i].TabIndex = 2; this.lbArray[i].Text = "lb"; this.Controls.Add( lbArray[i]); }

              memix

              P 1 Reply Last reply
              0
              • S sujithkumarsl

                It will better to use sortedList or hashTable to store the list box with the key as List box name. SortedList listBoxItems = new SortedList(); foreach(ListBox listBox ) { listBoxItems.Add(listBox); }

                My small attempt...

                P Offline
                P Offline
                Pete OHanlon
                wrote on last edited by
                #7

                But how does the listbox get added to the form? The OP talked about user created listboxes and displaying them on a form.

                Deja View - the feeling that you've seen this post before.

                1 Reply Last reply
                0
                • M memix

                  Dear friends, Thanks for all of you, I succeed it, it works what I want, at the end my solution is here: public int numberOfListBoxes =0; ListBox[] lbArray; private void button1_Click(object sender, EventArgs e) { numberOfListBoxes = Convert.ToInt32(textBox1.Text); lbArray = new ListBox[numberOfListBoxes]; int i; int listBoxDistances=100; for (i = 0; i < numberOfListBoxes; i++) { lbArray[i] = new ListBox(); this.lbArray[i].Location = new System.Drawing.Point(100+(i*listBoxDistances), 200); this.lbArray[i].Name = "lbArray"+"["+i+"]"; this.lbArray[i].Size = new System.Drawing.Size(57, 100); this.lbArray[i].TabIndex = 2; this.lbArray[i].Text = "lb"; this.Controls.Add( lbArray[i]); }

                  memix

                  P Offline
                  P Offline
                  Pete OHanlon
                  wrote on last edited by
                  #8

                  Can I add a small performance enhancement here? Don't add your listboxes to the control collection in each iteration. Instead, use Controls.AddRange to add the entire collection of listboxes.

                  Deja View - the feeling that you've seen this post before.

                  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