How to Validate the TextBox using ErrorProvider in VB.NET

1
1106

This tutorial is all about How to Validate the TextBox using ErrorProvider in VB.NET.
In this tutorial, I will teach you how to validate a Textbox using an ErrorProvider in VB.NET. This will help you know what are the inputs that you’re going to put in the TextBox and it will inform you if the errors will occur.

 

Let’s begin:
Open the Visual Basic and create a new Windows Form Application. Drag the two TextBoxes, a Button and an Error Provider in the Form. And it will look like this.
errorproviderform

 

After setting up the Form, double click the Button and put the codes into the click event handler of the Button.
[vbnet] Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
‘CHECKING IF THE TWO TEXTBOXES ARE CLEARED.
If txt_string.Text = “” And txt_integer.Text = “” Then
‘THE ERROR PROVIDER WILL APPEAR AND IT WILL INFORM THE PROBLEM TO THE USER.
ErrorProvider1.SetError(txt_string, “Textbox must be filled up.”)
ErrorProvider1.SetError(txt_integer, “Textbox must be filled up.”)
Else
‘SET THE ERROR PROVIDER TO BE CLEARED.
ErrorProvider1.SetError(txt_string, “”)
ErrorProvider1.SetError(txt_integer, “”)
‘CHECKING IF YOU HAVE INPUT A NUMERIC VALUE.
If IsNumeric(txt_string.Text) Then
‘THE ERROR PROVIDER WILL APPEAR AND IT WILL INFORM THE PROBLEM TO THE USER.
ErrorProvider1.SetError(txt_string, “You have input a numeric value that is not valid.”)
Else
‘SET THE ERROR PROVIDER TO BE CLEARED.

ErrorProvider1.SetError(txt_string, “”)
End If
‘CHECKING IF YOU HAVE INPUT A STRING VALUE.
If Not IsNumeric(txt_integer.Text) Then
‘THE ERROR PROVIDER WILL APPEAR AND IT WILL INFORM THE PROBLEM TO THE USER.
ErrorProvider1.SetError(txt_integer, “You have input a string value that is not valid.”)
Else
‘SET THE ERROR PROVIDER TO BE CLEARED.
ErrorProvider1.SetError(txt_integer, “”)
End If
End If

End Sub
[/vbnet] Run your project and click the Button to perform the code in the method.
Output:
errorprovideroutput_1

You can download the complete source code Here.

 

Readers might read also:

 

Facebook Comments

1 COMMENT

  1. […] How to Validate the TextBox using ErrorProvider in VB.NET – Oct 3, 2015. This tutorial is all about How to Validate the TextBox using ErrorProvider in VB. NET. In this tutorial, I will teach you how to validate a Textbox using. […]

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.