So I know how to do just the textboxes. But the validation code will skip within a groupbox, because the control is not within the form. I was trying to somehow call the specific groupbox form but I don't know the exact syntax. Sorry I am new at this, if I don't have enough info please let me know. Also if you have references to control or groupbox commands it would be helpful. thanks!
For Each cntrl As Control In Controls
If TypeOf cntrl Is TextBox Then
cntrl.BackColor = Color.White
If cntrl.Text = String.Empty Then
cntrl.BackColor = Color.Yellow
cntrl.Focus()
Return False
End If
For Each cntrl As GroupBox In Controls
If TypeOf cntrl Is TextBox Then
cntrl.BackColor = Color.White
If cntrl.Text = String.Empty Then
cntrl.BackColor = Color.Yellow
cntrl.Focus()
Return False
End If
As per my comments, proper WinForms validation should look like this:
Private Sub TextBoxes_Validating(sender As Object, e As CancelEventArgs) Handles TextBox3.Validating,
TextBox2.Validating,
TextBox1.Validating
Dim tb = DirectCast(sender, TextBox)
If tb.TextLength = 0 Then
tb.BackColor = Color.Yellow
e.Cancel = True
Else
tb.BackColor = SystemColors.Window
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If ValidateChildren() Then
'All controls contain valid data so go ahead and use it.
End If
End Sub
You will now validate all the controls whose Validating
event you handle, wherever they are located, and none that you don't. If you set CausesValidation
to False
on all the controls then no validation will occur until you call ValidateChildren
. Even if you do want to validate as you go, you should still call ValidateChildren
because that will ensure that even those controls that have never received focus will be validated.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.