-3

I have over 20 text boxes set up and what I'm trying to do is take the value out of each one of them and place it an array of values (structure). Each of my text boxes has a name of txtScore00, 01, 02 and so on and I'm trying to loop them with a counter and use the counter as numbers in my variable name. I found this topic Concatenate Variable Names in VB but i still cant get it to work. Any suggestions? My code populating the array:

For intCountOne = 0 To aStudent.Length - 1
        For intCountTwo = 0 To aStudent(intCountOne).decGrade.Length - 1
            aStudent(intCountOne).decGrade(intCountTwo) = Me.Controls("tbScore" & intCountOne & intCountTwo).Text
Community
  • 1
  • 1
LittleB0Y
  • 124
  • 1
  • 8

1 Answers1

1

You have:

Me.Controls("tbScore" & intCountOne & intCountTwo)

Which requires the TextBoxes to be directly contained by the Form. If they are in a different container, like a Panel, then you'd need to change "Me" to the name of the container (such as "Panel1").

Alternatively, you can search the Form for a control with that name, which is quite useful if the TextBoxes are spread out over multiple containers (it'll still work if they are all in the same container).

Here's a quick example:

    Dim matches() As Control
    For intCountOne = 0 To aStudent.Length - 1
        For intCountTwo = 0 To aStudent(intCountOne).decGrade.Length - 1
            matches = Me.Controls.Find("tbScore" & intCountOne & intCountTwo, True)
            If matches.Length > 0 AndAlso TypeOf matches(0) Is TextBox Then
                Dim tb As TextBox = DirectCast(matches(0), TextBox)
                aStudent(intCountOne).decGrade(intCountTwo) = tb.Text
            End If
        Next
    Next
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40
  • figured it out, the issue was that i was looping all of it within a group box, got rid of it and works perfecly – LittleB0Y Nov 20 '14 at 23:08
  • Yes...probably because of the reason I stated. I'm guessing you moved the controls from the GroupBox to the Form? If you use code like I posted above then it doesn't matter which container they are in as they will always be found. – Idle_Mind Nov 20 '14 at 23:13