Reading multipule textboxes

mikewhorley

New member
Joined
May 29, 2011
Messages
3
Programming Experience
Beginner
VB.NET:
Expand Collapse Copy
Dim test As New List(Of Integer)
For Each ctrl In Me.GroupBox1.Controls
If TypeOf ctrl Is TextBox Then
test.Add(Convert.ToByte(ctrl.Text))
RichTextBox1.AppendText(ctrl.name)
End If
Next
Dim i As Integer
For i = 0 To (test.Count - 1)
RichTextBox2.AppendText(test(i))
Next i

My question being what determins the order that the textboxes are read. Example I have 4 textboxes named textbox1 to textbox4 When the code is run the output in richtextbox1 is textbox4textbox3textbox2textbox1. The output in richtextbox2 is 4321.

So here textbox1 = test(3) and textbox4 = test(0)
 
It would be determined in the order which you added the controls. I believe that the only way to change this is to either modify the Designer.VB file or delete and re-add the textboxes.
 
I would think it would be the other way around, but I don't know for sure.
 
My question being what determins the order that the textboxes are read.
For Each...Next Statement (Visual Basic)
If your code depends on traversing a collection in a particular order, a For Each...Next loop is not the best choice, unless you know the characteristics of the enumerator object the collection exposes. The order of traversal is not determined by Visual Basic, but by the MoveNext method of the enumerator object. Therefore, you might not be able to predict which element of the collection is the first to be returned in element, or which is the next to be returned after a given element. You might achieve more reliable results using a different loop structure, such as For...Next or Do...Loop.
It is also true which is said that For Each on a collection returns in order the items was added to the collection, which for control designer can be any order and may change each time designer generated code is regenerated. As explained if you rely on order use a For-Next loop and look up each control by ordered name. You can also maintain your own collection/array of objects in a particular order and do For Each on that.
 
Back
Top