Validating Multiple Controls in VB.NET
If you have ever built a form in Visual Studio with say 10 textboxes, you then want to validate all 10 textboxes on a certain event, well naturally you could go through each one like so
If txtTextBox1.Text = "" then
End If
If txtTextBox1.Text = "" then
End If
If txtTextBox1.Text = "" then
End If
etc.......
But this leads to messy and tedious code so below is an example on how to loop through controls on a form and validate them on the way.
For Each ctrl As Control In Me.Controls
If ctrl.Text = "" Then
MessageBox.Show("Invalid input for " & ctrl.Name)
Exit Sub
End If
Next
Ok so this validates all the controls on the form which may not be what we are after so how about checking the type of the control inside the loop? This way we can validate only textboxes or only combo boxes as we need, but what if we only want to validate so many of our text boxes? Keep reading!
For Each ctrl As Control In Me.Controls
If TypeOf ctrl Is TextBox Then
If ctrl.Text = "" Then
MessageBox.Show("Invalid input for " & ctrl.Name)
Exit Sub
End If
End If
Next
So now we know how to check different types of controls but what if I have 6 Textfields that are string and then another 4 that are phone numbers or numeric values? Well if we name the controls correctly we could check against any control we like based on their name, this allows us to group controls for specific validation tasks.
For Each ctrl As Control In Me.Controls
If TypeOf ctrl Is TextBox Then
'check if it should be string
If ctrl.Name.StartsWith("txtString") Then
'check if its valid value
If ctrl.Text = "" Then
'if its blank exit sub
MessageBox.Show("Invalid Input")
Exit Sub
End If
'check if it should be numeric
ElseIf ctrl.Name.StartsWith("txtInt") Then
'validate that it is numeric
If Not IsNumeric(ctrl.Text) Then
'if not show error and exit sub
MessageBox.Show("Please enter Numeric Values for Phone Numbers")
Exit Sub
End If
End If
End If
Next
You can see that for controls where we want string values entering, we name them txtString and for controls where we want numeric values we name them txtInt. This is only a simple example and you could take it further by looking at the rest of the control name say txtStringAddress which would allow you to give more friendly error messages specific to the correct field.
PS: this is only some code i made up for a simple task I tackled the other day, if anyone else has their own validation snippets please share them.
Comments
Never thought of validating controls by name... good job!
Good one, i was trying almost the same thing but including an errorProvider to avoid message Box popping for every error and likely annoying the user.. So please review the code below and advise why this doesnt always show the errorProvder when there is no input
The code looks fine for to me. Are you saying that errorProvider.GetError() does not return "Required field" when some fields are empty? I can't see you outputting the value anywhere?
Try the documentation http://msdn.microsoft.com/en-us/library/system.windows.forms.errorprovider.aspx#Y3115
Also you may want to change ErrorProvider1.SetError(c, "") to ErrorProvider1.SetError(c, String.Empty)
in which event a should put the code ?
@dee, the click event of a submission button.
So... this works great except for it won't work on my last record. Any ideas?
Add new comment