Dynamic control names

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a form that has 156 list boxes on it. Is there a way to refer to each on in a for loop to populate their data instead of writing code for each one

for example

for(int x;x < 156;x++
listBox x = somethin

instead of
listBox1 = somethin
listBox2 = somethin
...
listBox156 = something
 
=?Utf-8?B?RXJpYw==?= said:
I have a form that has 156 list boxes on it. Is there a way to refer to each
on in a for loop to populate their data instead of writing code for each one?

for example:

for(int x;x < 156;x++)
listBox x = something

instead of
listBox1 = something
listBox2 = something
...
listBox156 = something

Yes - use an array (or ArrayList) instead to start with. It's not as
"designer-friendly" but *much* more maintenance-friendly.
 
Hi eric,

You can keep them referenced on a ArrayList the thing is how to know what
to place on each one, you have to think about it,
if you want to use the designer and still be able to treat them as a whole
you could iterate in the Controls collection after the controls are created
and if the control is a ListBox add it to the ArrayList.

One advice, the loading of this form can be very slow, as the designer code
may not be the best way for doing so. if you do not need keep an individual
reference I would recommend you to change the code of the designer, once you
finish designing it.

Hope this help,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

Eric said:
I have a form that has 156 list boxes on it. Is there a way to refer to
each on in a for loop to populate their data instead of writing code for
each one?
 
Eric said:
I have a form that has 156 list boxes on it. Is there a way to refer
to each on in a for loop to populate their data instead of writing
code for each one?

You could loop through the control array on the form, picking out the
listboxes.
 
Back
Top