Click here to Skip to main content
15,884,629 members
Articles / Programming Languages / C#
Tip/Trick

Error Provider Control in Windows Application Form

Rate me:
Please Sign up or sign in to vote.
4.31/5 (11 votes)
25 Sep 2013CPOL2 min read 60.2K   3.9K   15   1
How to use error provider control in Windows Form application

Introduction

In this tip and trick, I am going to show you how to use error provider control in Windows Form application(C#).

Actually, the error provider alerts the user that something is wrong.

In this example, I will use a Display Warning icon, wrong icon (when an incorrect expression is entered or a Tick icon depending upon the data entered in the text box so that the user can determine that data entered is correct or incorrect.

Let's Start

  1. Create a new Windows Forms Application.
  2. Place two text boxes (for getting name and age) and three ErrorProvider controls from the toolbox.

    Image 1

    Set text boxes are named txt_name and txt_age. I have also created a button but there is no use for it (in the example).

    Image 2

  3. Set the Icon for errorProvider1 (warning), errorProvider2 (wrong or cross) and errorProvider3 as a tick mark (for a correct entry). I will use the warning errorprovider when the user leaves a text box blank, use a cross or wrong when the user enters incorrect format (for the Age text box) and a tick icon for when all conditions are satisfied.
  4. In addition to defaults imports, I am adding Regular Expression library support for the txt_age text box as in the following:
    C#
    using System.Text.RegularExpressions; 
  5. Now we need to create validating events for both text boxes. Right-click on the TextBox, then click on "Properties". Then click on the lightening bolt (Events) icon at the top of the window, then double-click on validating event.
  6. Enter these codes between the generated codes:
    C#
    void Txt_nameValidating(object sender, System.ComponentModel.CancelEventArgs e)
    {
    if(txt_name.Text==string.Empty)
    {
    errorProvider1.SetError(txt_name,"Please Enter Name");
    errorProvider2.SetError(txt_name,"");
    errorProvider3.SetError(txt_name,"");
    }
    else
    {
    errorProvider1.SetError(txt_name,"");
    errorProvider2.SetError(txt_name,"");
    errorProvider3.SetError(txt_name,"Correct");
    }
    }
    void Txt_ageValidating(object sender, System.ComponentModel.CancelEventArgs e)
    {
    if(txt_age.Text==string.Empty)
    {
    errorProvider1.SetError(txt_age,"Please provide age");
    errorProvider2.SetError(txt_age,"");
    errorProvider3.SetError(txt_age,"");
    }
    else
    {
    Regex numberchk=new Regex(@"^([0-9]*|\d*)$");
    if(numberchk.IsMatch(txt_age.Text))
    {
    errorProvider1.SetError(txt_age,"");
    errorProvider2.SetError(txt_age,"");
    errorProvider3.SetError(txt_age,"Correct");
    }
    else
    {
    errorProvider1.SetError(txt_age,"");
    errorProvider2.SetError(txt_age,"Wrong format");
    errorProvider3.SetError(txt_age,"");
    }
    } 
    } 

    In the "txt_nameValidating()" event, if txt_name is left empty, then it shows error. This is done using the "setError()" method. The setError method sets the error description string for the specified control. If the user hovers the mouse over the error icon, then it shows an error description string that we have declared in the setError() method.

  7. Build and run the code.

Preview

Image 3

You can also download the source code of this example.

Hope you like it.

Thanks.

My Other Tips and Tricks

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Student
India India

Comments and Discussions

 
SuggestionEmbed code in a class for effective reuse Pin
jack_griffin12-Mar-16 22:56
jack_griffin12-Mar-16 22:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.