Terry Bottorff (02/18/2008)
OK I understand and that is OK since I got it working Trents way and that is cool. But, is there a way to put this in a loop instead of typing them all?
myControls.Add(txt1)
myControls.Add(txt2)
myControls.Add(txt3)
I tried this but it would not work
for cnt = 1 to 40
cntrl = "txt" & cnt.tostring()
myControls.Add(cntrl)
next cnt
Thanks again.You need to put the actual controls into the list/array, rather than the name of the control. I.e. in you top example, you are putting the actual control into the collection and within the loop you are putting the
name into the collection. If you use the method Trent/Ben are suggesting, you will need to put them in individually, then after that you can easily loop through the collection to do whatever you need to them. Best bet would be to have a private method to just load them, then you have one place to update if you change the controls being loaded. If you want to loop, then you need to know what container they are in and you could use the method I suggested, to use a loop to build the names. A third option would be to loop, create the controls (not the name, but the actual control), add them to your collection and add them to the appropriate container on the form.
After reading this, I'd suggest you definitely use your own collection to do manage the controls. As mentioned, you can get into trouble if the controls are in different containers etc. If you use your own collection and then have a method to load that collection, you can then mess with how you are loading it, but the rest of your code is unaffected. Hope that makes sense.