I'm trying to collect up a number of text boxes and test against what is entered. I know this has been answered several times but most cases want to collect all the controls in a panel or form, I want to choose each text box.
I'm trying this, based on another stack overflow answer Loop through textboxes in vb.net but it not working for me. Null reference exception, which presumably I've not instantiated my object but I have I not, with the new keyword.... In summary why does this not work?
Dim boxes As New List(Of TextBox)() From {TextBox1, TextBox2, TextBox3, TextBox4, TextBox5}
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
TestTextBox()
End Sub
Private Sub TestTextBox()
For Each tb As TextBox In boxes
If String.IsNullOrEmpty(tb.Text) Then
MessageBox.Show("Text box is empty")
ElseIf tb.Text.Length > 10 Then
MessageBox.Show("Characters limited to 10")
End If
Next
End Sub