Looping through controls....

J

johnb41

I need to loop through a bunch of textbox controls on my form. The
order of the loop is very important. For example, the top one must be
read first, then the one below it, etc.

My first attempt was to put the controls in a Panel, and then loop
through the controls collection of the panel. What happened is it read
the controls from the bottom of my form to the top!

So I tried a more manual process. I named each textbox like this:

Textbox_1
Textbox_2
Textbox_3

Then tried a loop unsuccessfully:

For i as integer = 0 to 10
If Textbox_ & i.Text <> "" Then
'do something
End If
Next

This isn't exactly what i'm doing, but the idea is the same. Textbox_ &
i is causing an error because vb is reading it as a string. I need it
to be a control.

I know i'm missing something very obvious, but I just can't figure this
out!

Any ideas?
Thanks for all your help!

John
 
C

Carlos J. Quintero [.NET MVP]

Hi John,

I'd suggest an automatic approach, not a manual approach: Iterate all then
controls putting them into an array, and sort the array with the Sort method
passing a custom IComparer class that you provide. In this comparer you take
2 controls and return the comparison, based on their Top/Left properties.
After the array is sorted, you iterate them again.

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
You can code, design and document much faster.
Free resources for add-in developers:
http://www.mztools.com
 
C

Cor Ligthert

John
I need to loop through a bunch of textbox controls on my form. The
order of the loop is very important. For example, the top one must be
read first, then the one below it, etc.
You make me curious about the text above. What is the reason for that.
They are not visible.

On the other hand have I the idea that looping though them will cost you
more code than access them individualy.

You can put them all in an array and than access them looping through that
Dim mycontrolArray() as control = {myTextBox1,MyButton2 etc}

for each ctr in myControlArray
ctr.Text = "whatever"
next

However probably as I said more work than access them individually.

I hope this helps,

Cor


Cor
 
J

johnb41

Cor,

Your solutions seems to make the most sense for my project. I'll try
it out, thanks!

John
 
G

G.Ashok

Dear Herfried K. Wagner,


The code in the link you specified has a bug in it. What happened if the
specified named control is not found! It will return a null vlaue so each
you should check for null :)

Regards,
Ashok



| >I need to loop through a bunch of textbox controls on my form.
|
| Accessing controls by their names or indices
| <URL:http://dotnet.mvps.org/dotnet/faqs/?id=controlbynameindex&lang=en>
|
| --
| M S Herfried K. Wagner
| M V P <URL:http://dotnet.mvps.org/>
| V B <URL:http://classicvb.org/petition/>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top