0

I have a couple arrays of textboxes at the form class level. When I attempt to loop through these arrays with a for each loop, my loop variables value is 'Nothing'. Here is my form level arrays:

Public Class frmTDSNew

' Form level arrays to hold text boxes to fill and validate
Dim decimalBoxes() As TextBox = New TextBox() {txtLenPerDrum, txtWeightPerDrum, txtEnamThick, txtEnamTolPos, txtEnamTolNeg,
                                 txtEpoxyThick, txtEpoxTolPos, txtEpoxTolNeg, txtNomDimH, txtNomDimHPos, txtNomDimHNeg,
                                 txtNomDimW, txtNomDimWPos, txtNomDimHNeg, txtTensileStrength, txtElongation, txtYieldStghMin,
                                 txtYieldStghMax, txtBulging, txtInsThick1, txtInsThick2, txtInsThick3, txtProThick, txtEdgeProThick,
                                 txtInterColThick}

Dim integerBoxes() As TextBox = New TextBox() {txtPONbr, txtNbrWire, txtNbrDrum, txtDrumSets, txtWindInnerDia, txtPitch}

And here is one of the places I am trying to loop through the arrays:

Private Function IsValidData() As Boolean

    For Each box As TextBox In decimalBoxes
        If Not IsDecimal(box) Then
            Return False
        End If
    Next

    For Each box As TextBox In integerBoxes
        If Not IsInt32(box) Then
            Return False
        End If
    Next

    Return True

End Function

In the loops, 'box' is set to nothing, even though when I set a breakpoint while in debug mode I can see that everything in my arrays are definitely textboxes. Everything works fine when I move the arrays to within the functions I am calling them, but this defeats the purpose of declaring them at the class level. I want to use these arrays in several places on my form. Any idea on what I am doing wrong here?

2
  • 1
    you should probably only add the TBs to the array after the form designer code runs and creates them. seems like that code should throw an exception. IsDecimal(box) should probably be IsDecimal(box.Text) since you want to test the TB contents Commented May 7, 2014 at 18:09
  • You're right on the nail about adding them to the array after the form designer code. I changed my code to declare the array at the class level, and assign the texboxes to the array in my frmTDSNew_Load event. If you want to make this an answer I'll accept it. Thanks! Commented May 7, 2014 at 18:23

2 Answers 2

1

With this code:

Public Class frmTDSNew

' Form level arrays to hold text boxes to fill and validate
Dim decimalBoxes() As TextBox = New TextBox() {txtLenPerDrum, txtWeightPerDrum,
          txtEnamThick, txtEnamTolPos, txtEnamTolNeg,
          txtEpoxyThick, txtEpoxTolPos, txtEpoxTolNeg, txtNomDimH, 
          txtNomDimHPos, txtNomDimHNeg,

None of the controls exist yet - the designer did not create them yet, so its the wrong place to add them. At least move it to Sub New or FormLoad

...or remove it altogether:

Another way to do this, is to use a List(of String) containing the names:

Private decList As New List(Of String)
'...

decList.Add("txtLenPerDrum")
decList.Add("txtWeightPerDrum")    

Then to test:

For n as Integer = 0 to decList.Count -1
    If Not IsDecimal(Controls(decList(n)).Text) Then
        Return False
    End If
Next n

You can also "automate" it by having the desired controls "subscribe", or add themselves to the list:

Private Sub TextBox1_HandleCreated(sender As Object, e As EventArgs) 
        Handles TextBox1.HandleCreated

   myList.Add(TextBox1.Name)

End Sub

Note: HandleCreated is called the first time the control is shown, so if there are some that are invisible until a certain time, they wont be in the List until shown.

Sign up to request clarification or add additional context in comments.

Comments

1

You should be declaring an array like this:

Dim decimalBoxes As TextBox()

1 Comment

Right you are. I was still getting errors after fixing this though. Plutonix's comment lead me to the answer however. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.