By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,018 Members | 1,204 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,018 IT Pros & Developers. It's quick & easy.

Looping through controls....

P: n/a
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

Nov 21 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"johnb41" <or****@informatik.com> schrieb:
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/>
Nov 21 '05 #2

P: n/a
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

"johnb41" <or****@informatik.com> escribió en el mensaje
news:11*********************@f14g2000cwb.googlegro ups.com...
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

Nov 21 '05 #3

P: n/a
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
Nov 21 '05 #4

P: n/a
Cor,

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

John

Nov 21 '05 #5

P: n/a
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

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:eU**************@TK2MSFTNGP15.phx.gbl...
| "johnb41" <or****@informatik.com> schrieb:
| >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/>
Nov 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.