How to Validate the TextBox using ErrorProvider in VB.NET

0
614

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.

  1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  2. 'CHECKING IF THE TWO TEXTBOXES ARE CLEARED.
  3. If txt_string.Text = "" And txt_integer.Text = "" Then
  4. 'THE ERROR PROVIDER WILL APPEAR AND IT WILL INFORM THE PROBLEM TO THE USER.
  5. ErrorProvider1.SetError(txt_string, "Textbox must be filled up.")
  6. ErrorProvider1.SetError(txt_integer, "Textbox must be filled up.")
  7. Else
  8. 'SET THE ERROR PROVIDER TO BE CLEARED.
  9. ErrorProvider1.SetError(txt_string, "")
  10. ErrorProvider1.SetError(txt_integer, "")
  11. 'CHECKING IF YOU HAVE INPUT A NUMERIC VALUE.
  12. If IsNumeric(txt_string.Text) Then
  13. 'THE ERROR PROVIDER WILL APPEAR AND IT WILL INFORM THE PROBLEM TO THE USER.
  14. ErrorProvider1.SetError(txt_string, "You have input a numeric value that is not valid.")
  15. Else
  16. 'SET THE ERROR PROVIDER TO BE CLEARED.
  17.  
  18. ErrorProvider1.SetError(txt_string, "")
  19. End If
  20. 'CHECKING IF YOU HAVE INPUT A STRING VALUE.
  21. If Not IsNumeric(txt_integer.Text) Then
  22. 'THE ERROR PROVIDER WILL APPEAR AND IT WILL INFORM THE PROBLEM TO THE USER.
  23. ErrorProvider1.SetError(txt_integer, "You have input a string value that is not valid.")
  24. Else
  25. 'SET THE ERROR PROVIDER TO BE CLEARED.
  26. ErrorProvider1.SetError(txt_integer, "")
  27. End If
  28. End If
  29.  
  30. End Sub

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
(Visited 354 times, 1 visits today)

LEAVE A REPLY

Please enter your comment!
Please enter your name here