ErrorProvider In WinForms And Windows Forms

1:52:00 am 0 Comments

ErrorProvider In WinForms or Windows Forms application using C# and VB.NET

In this post i am going to describe how to use error provider control in winforms or windows forms application using C# and VB.NET.

ErrorProvider in winforms and windows forms applications
I am using error provider control to display warning or tick icon depending on data entered in textbox so that user can find out text entered is correct or incorrect.

in first textbox i am just checking whether it's empty or not.

Second textbox is numeric only, user can enter only numbers in this and if anything other than number is entered, error provider will show warning icon beside textbox with tooltip containing suggestion.
I have used regular expression to check textbox text for numbers.


For this i have created a simple winform application with 2 textbox on windows forms. follow steps mentioned below for this example.

1. Create new windows application in visual studio.

2. On the form place 2 textbox and 2 errorprovider control from toolbox.

I m using 2 errorproviders, one to display warning icon and other to displat tick or success icon.


Add this namespace in code behind of form to use regex.
1using System.Text.RegularExpressions;

now generate Validated or Validating event for both textboxes by opening property windows of textbox and clicking on lightning icon (Events) at the top of window. from there scroll to bottom and double click on validating. it will generate validating event for textbox in code behind.

Write code mentioned below in events generated.

C# code

01private void textBox1_Validating(object sender, CancelEventArgs e)  
02        {
03            if (textBox1.Text == string.Empty)
04            {
05                errorProvider1.SetError(textBox1, "Please Enter Name");
06                errorProvider2.SetError(textBox1, "");
07            }
08            else
09            {
10                errorProvider1.SetError(textBox1, "");
11                errorProvider2.SetError(textBox1, "correct");
12            }
13        }
14 
15        private void textBox2_Validated(object sender, EventArgs e)
16            {
17            if (textBox2.Text == string.Empty)
18            {
19                errorProvider1.SetError(textBox2, "please enter age");
20                errorProvider2.SetError(textBox2, "");
21            }
22            else
23            {
24                Regex NumericOnly;
25                NumericOnly = new Regex(@"^([0-9]*|\d*)$");
26                if (NumericOnly.IsMatch(textBox2.Text))
27                {
28                    errorProvider1.SetError(textBox2, "");
29                    errorProvider2.SetError(textBox2, "correct");
30                }
31                else
32                {
33                    errorProvider1.SetError(textBox2, "Please Enter only numbers");
34                    errorProvider2.SetError(textBox2, "");
35                }
36            }
37        }

VB.NET Code

01Private Sub textBox1_Validating(sender As Object, e As CancelEventArgs)
02 If textBox1.Text = String.Empty Then
03  errorProvider1.SetError(textBox1, "Please Enter Name")
04  errorProvider2.SetError(textBox1, "")
05 Else
06  errorProvider1.SetError(textBox1, "")
07  errorProvider2.SetError(textBox1, "correct")
08 End If
09End Sub
10 
11Private Sub textBox2_Validated(sender As Object, e As EventArgs)
12 If textBox2.Text = String.Empty Then
13  errorProvider1.SetError(textBox2, "please enter age")
14  errorProvider2.SetError(textBox2, "")
15 Else
16  Dim NumericOnly As Regex
17  NumericOnly = New Regex("^([0-9]*|\d*)$")
18  If NumericOnly.IsMatch(textBox2.Text) Then
19   errorProvider1.SetError(textBox2, "")
20   errorProvider2.SetError(textBox2, "correct")
21  Else
22   errorProvider1.SetError(textBox2, "Please Enter only numbers")
23   errorProvider2.SetError(textBox2, "")
24  End If
25 End If
26End Sub

Build and run the application.


Download Sample Code

0 comments: