User Input Validations using ErrorProvider in VB.NET

This article and attached project demonstrates how to use the ErrorProvider component in Windows Forms and VB.NET to implement user input validations.
  • 12108
 

User Input Validations in Windows Forms

Now, I am going to create an application in Windows Forms that uses the ErrorProvider component for implementing user input validations and display proper error messages when the data is invalid.

I have a Windows Forms application that looks like following and has two input controls. One is Author Name TextBox and the second is Age NumericUpDown control.


UserInputValidationImg1.jpg

My goal here is to display error messages on the Submit Data button click event handler when Author Name is not specified and age is less than 21.

For this, I add two ErrorProvider components to the Form by dragging and dropping them from the ToolBox to the Form. I change their names to NameErrorProvider and AgeErrorProvider. Now, on the Button click event handler, I validate name and age and if the data is not valid, I display error messages in the ErrorProviders by calling the ErrorProvider.SetError method.
The complete code is listed below.

Public Class Form1

 

    Private Sub SubmitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SubmitButton.Click

 

        ' Validate Author Name

        Dim err As String = ValidateAuthorName(AuthorNameTextBox.Text)

        If (err = String.Empty) Then

            NameErrorProvider.SetError(AuthorNameTextBox, "")

        Else

            NameErrorProvider.SetError(AuthorNameTextBox, err)

        End If

 

        ' Validate Author Age

        err = ValidateAuthorAge(AuthorAgeUpDown.Value)

        If (err = String.Empty) Then

            AgeErrorProvider.SetError(AuthorAgeUpDown, "")

        Else

            AgeErrorProvider.SetError(AuthorAgeUpDown, err)

        End If

 

    End Sub

 

    ''' <summary>

    ''' Verifies author name

    ''' </summary>

    ''' <returns></returns>

    ''' <remarks></remarks>

    Private Function ValidateAuthorName(ByVal name As String) As String

        Dim errorMessage As String = String.Empty

        If name.Length <= 0 Then

            errorMessage = "Author name is required."

        End If

        Return errorMessage

    End Function

 

    ''' <summary>

    ''' Verifies author age

    ''' </summary>

    ''' <returns></returns>

    ''' <remarks></remarks>

    Private Function ValidateAuthorAge(ByVal age As Int16) As String

        Dim errorMessage As String = String.Empty

        If age < 21 Then

            errorMessage = "Age must be less than 21."

        End If

        Return errorMessage

    End Function

 

  

End Class

 
Now when you run the application and provide invalid data and click Submit Data button, you will see ErrorProviders appear like following and rollover on them displays the error messages.


UserInputValidationImg2.jpg

Summary

In this article, we saw how we can take advantages of built-in component in Windows Forms to provide user input validation functionality. Download the attached project for more details.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.