How to iterate through many text boxes??

W

weg22

Hi all,

I'm trying to iterate through several text boxes and perform
calculations with the data. I'm using the following code which
compiles and executes successfully...but only does half of what I need
it to:

For Each ctrl In Controls
If TypeOf (ctrl) Is System.Windows.Forms.TextBox Then

MSB = Val(ctrl.Text) >> 8
LSB = Val(ctrl.Text) - MSB * 256

End If
Next

If I print the MSB and LSB values, it appears that the above code is
looping through the Text Boxes in a different order than I need it
to. Instead of rearranging all of the Text Boxes in my application,
is there away to define the "ctrl" value for each text box (i.e. can I
specify the order of how to loop through eact Text Box)?

Also, if I move these Text Boxes onto a "TabControl", the above code
no longer works. Can someone please explain this as well?

Thanks in advance,
-weg22 ([email protected])
 
J

Jack Jackson

Hi all,

I'm trying to iterate through several text boxes and perform
calculations with the data. I'm using the following code which
compiles and executes successfully...but only does half of what I need
it to:

For Each ctrl In Controls
If TypeOf (ctrl) Is System.Windows.Forms.TextBox Then

MSB = Val(ctrl.Text) >> 8
LSB = Val(ctrl.Text) - MSB * 256

End If
Next

If I print the MSB and LSB values, it appears that the above code is
looping through the Text Boxes in a different order than I need it
to. Instead of rearranging all of the Text Boxes in my application,
is there away to define the "ctrl" value for each text box (i.e. can I
specify the order of how to loop through eact Text Box)?

Also, if I move these Text Boxes onto a "TabControl", the above code
no longer works. Can someone please explain this as well?

Thanks in advance,
-weg22 ([email protected])

'Controls' is a collection property of the container (probably a form)
that contains the list of controls that are directly on the container.
To see controls that are children of some other control (panel,
TabControl page, etc.) you would need to use the Controls collection
of their parent.

Every control has a GetNextControl() method that returns a reference
to the next control in the tab order. If tab order would work for
you, you could start with the first textbox and use GetNextControl()
to find the others.
 
S

Stephany Young

A far better idea than relying on the TabOrder, in my opinion, would be to
use a array with some smarts ...

Enter the Generic List(Of T).

To use it, first you would need a module (i.e. Form) level variable:

Private m_textboxes As List(of TextBox)

In the Load event handler for the Form, instantiate and populate the
variable with the TextBox controls of interset in the sequence YOU want
them:

m_textboxes = New List(of TextBox)

m_textboxes.Add(TextBox1)

m_textboxes.Add(TextBox2)

m_textboxes.Add(TextBox3)

m_textboxes.Add(TextBox4)

or, instead:

m_textboxes = New List(of TextBox)(New TextBox() {TextBox1, TextBox2,
TextBox3, TextBox4})

When you need to deal with them, iterate the elements of the Generic List,
to achieve what you need to achieve:

For Each _textbox As TextBox In m_textboxes
MSB = Val(_textbox .Text) >> 8
LSB = Val(_textbox .Text) - MSB * 256
Next
 
M

Martin H.

Hello,
I'm trying to iterate through several text boxes and perform
calculations with the data. I'm using the following code which
compiles and executes successfully...but only does half of what I need
it to:

For Each ctrl In Controls
If TypeOf (ctrl) Is System.Windows.Forms.TextBox Then

MSB = Val(ctrl.Text) >> 8
LSB = Val(ctrl.Text) - MSB * 256

End If
Next

Alternatively you could make an array of text boxes and then loop
through it.

Best regards,

Martin
 

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