Sunday 29 November 2015

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.

If In Form textbox is within the Groupbox or Panal u can use like:


         For Each ctrl As Control In Me.Controls
                If CheckControlValidation(ctrl) = False Then
                    Exit Sub
                End If
         Next
 Private Function CheckControlValidation(ByVal cnt As Control) As Boolean
     Try
        If TypeOf cnt Is GroupBox  or  TypeOf cnt Is Panel Then
           For Each control As Control In cnt.Controls
                    'Loops through array of controls
              If TypeOf control Is TextBox Then
                   If control.Text = "" Then
                       MessageBox.Show("Invalid input for " & control.Name)
                       control.Focus()
                       Return False
                   End If
               End If
           Next

        ElseIf TypeOf cnt Is TextBox Then

          'check if it should be string
          If cnt.Name.StartsWith("txtString") Then
                 'check if its valid value
              If cnt.Text = "" Then
                'if its blank exit sub
                  MessageBox.Show("Invalid Input")
                  cnt.Focus()
                  Return False
                  End If
            'check if it should be numeric
           ElseIf cnt.Name.StartsWith("txtInt") Then
                    'validate that it is numeric
            If Not IsNumeric(cnt.Text) Then
                        'if not show error and exit sub
               MessageBox.Show("Please enter Numeric Values for Phone Numbers")
               cnt.Focus()
               Return False
            End If
          End If
       End If
       Return True
            
   Catch ex As Exception
            MbErr(ex)
    End Try
End Function
Thank You...

No comments:

Post a Comment