Textbox Validation using Error Provider in C#

Today in C#, I will teach you how to create a program that will validate an inputted textbox using ErrorProvider. In this tutorial, you will know what are the inputs you are going to put in the textbox and it will notify you whatever errors that you might encounter. Now, let's start this tutorial! 1. Let's start with creating a Windows Form Application in C# for this tutorial by following the following steps in Microsoft Visual Studio 2010: Go to File, click New Project, and choose Windows Application. 2. Add 2 textbox named txtstring by inputting string/text and txtnumber to input numbers only. Then insert one button named Button1. Design your interface like this: design 3. Now put this code in your Button1_Click to validate the textbox.
  1. private void Button1_Click(System.Object sender, System.EventArgs e)
  2. {
  3.  
  4. //CHECKING IF THE TWO TEXTBOXES ARE CLEARED
  5. if (txtstring.Text == "" && txtnumber.Text == "")
  6. {
  7. ErrorProvider1.SetError(txtstring, "Field must be filled up.");
  8. ErrorProvider1.SetError(txtnumber, "Field must be filled up.");
  9. }
  10. else
  11. {
  12. //THE ERROR PROVIDER WILL BE CLEARED.
  13. ErrorProvider1.SetError(txtstring, "");
  14. ErrorProvider1.SetError(txtnumber, "");
  15.  
  16. //CONDITION FOR THE STRING VALUE
  17. //CHECK IF THE INPUT IS A NUMERIC VALUE THEN PERFORM THE ERROR PROVIDER
  18. //IF NOT, CLEAR THE ERROR PROVIDER.
  19. if (IsNumeric(txtstring.Text))
  20. {
  21. //THE ERROR PROVIDER WILL APPEAR AND WILL NOTIFY THE PROBLEM TO THE USER.
  22. ErrorProvider1.SetError(txtstring, "Input string value is not valid.");
  23. }
  24. else
  25. {
  26. //THE ERROR PROVIDER WILL BE CLEARED.
  27. ErrorProvider1.SetError(txtstring, "");
  28. }
  29.  
  30. //CONDITION FOR THE NUMERIC VALUE
  31. //CHECK IF THE INPUT IS NOT A NUMERIC VALUE THEN PERFORM THE ERROR PROVIDER
  32. //IF NUMERIC, CLEAR THE ERROR PROVIDER.
  33. if (!IsNumeric(txtnumber.Text))
  34. {
  35. //THE ERROR PROVIDER WILL APPEAR AND WILL NOTIFY THE PROBLEM TO THE USER.
  36. ErrorProvider1.SetError(txtnumber, "Input numeric value is not valid.");
  37. }
  38. else
  39. {
  40. //THE ERROR PROVIDER WILL BE CLEARED.
  41. ErrorProvider1.SetError(txtnumber, "");
  42. }
  43.  
  44. }
  45.  
  46. }

Output:

output For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below. Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer If you have some queries, feel free to contact the number or e-mail below. Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add new comment