How can I build a ListBox array?
-
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
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 helpsDo your best to be the best
-
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
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
-
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
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.
-
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.
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...
-
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
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
-
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...
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.
-
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
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.