Ive got a Form application in VB.NET.
I have many text boxes on one form (about 20). Is there anyway to check them all at once to see if they are empty instead of wr
I would recommend using the Validating event of the TextBox controls, with an error provider control (just add one to your form):
Private Sub TextBox_Validating( sender As System.Object, e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating, TextBox2.Validating, ComboBox1.Validating
Dim ctl As Control = CType(sender, Control)
If ctl.Text = ""
e.Cancel = True
ErrorProvider1.SetError(ctl,"Please enter a value")
End If
End Sub
Then you can just call:
ErrorProvider1.Clear()
If Me.ValidateChildren()
' continue on
End If
The nice thing about this is that the user is informed about which textbox is missing and required. This works with other controls besides textboxes, so you can provide a more complete solution. Also, if you get to a later point where one or two textboxes don't need to have values, you simply do not validate them instead of having to add special cases in your loops.
Finally, if you don't want to type out all of the controls, then you could do this in form load:
For Each c As Control In Me.Controls
If TypeOf(c) is TextBox or TypeOf(c) is ComboBox
AddHandler c.Validating, AddressOf Me.TextBox_Validating
End If
Next