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.
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.
using 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
private void textBox1_Validating(object sender, CancelEventArgs e) { if (textBox1.Text == string.Empty) { errorProvider1.SetError(textBox1, "Please Enter Name"); errorProvider2.SetError(textBox1, ""); } else { errorProvider1.SetError(textBox1, ""); errorProvider2.SetError(textBox1, "correct"); } } private void textBox2_Validated(object sender, EventArgs e) { if (textBox2.Text == string.Empty) { errorProvider1.SetError(textBox2, "please enter age"); errorProvider2.SetError(textBox2, ""); } else { Regex NumericOnly; NumericOnly = new Regex(@"^([0-9]*|\d*)$"); if (NumericOnly.IsMatch(textBox2.Text)) { errorProvider1.SetError(textBox2, ""); errorProvider2.SetError(textBox2, "correct"); } else { errorProvider1.SetError(textBox2, "Please Enter only numbers"); errorProvider2.SetError(textBox2, ""); } } }
VB.NET Code
Private Sub textBox1_Validating(sender As Object, e As CancelEventArgs) If textBox1.Text = String.Empty Then errorProvider1.SetError(textBox1, "Please Enter Name") errorProvider2.SetError(textBox1, "") Else errorProvider1.SetError(textBox1, "") errorProvider2.SetError(textBox1, "correct") End If End Sub Private Sub textBox2_Validated(sender As Object, e As EventArgs) If textBox2.Text = String.Empty Then errorProvider1.SetError(textBox2, "please enter age") errorProvider2.SetError(textBox2, "") Else Dim NumericOnly As Regex NumericOnly = New Regex("^([0-9]*|\d*)$") If NumericOnly.IsMatch(textBox2.Text) Then errorProvider1.SetError(textBox2, "") errorProvider2.SetError(textBox2, "correct") Else errorProvider1.SetError(textBox2, "Please Enter only numbers") errorProvider2.SetError(textBox2, "") End If End If End SubBuild and run the application.
0 comments:
Post a Comment