Data Entry Validation with Error Provider

To use this code, simply drag the errorProvider control onto the form, it appears in the list as a red exclamation mark. Add one textbox and one command button, now use this code in the button click event

private void button1_Click(object sender, System.EventArgs e)
{
    //CHECK FOR DATA ENTRY
    if(textBox1.Text =="")
    {
        //NO DATA!
        errorProvider1.SetError(textBox1,"You need to enter a value");
    } else {
        //DATA ENTERED DONT SHOW THE ERROR WITH ""
        errorProvider1.SetError(textBox1,"");
    }
}


You set the error icon active if the data validation is not meet, in this case its if there is no text in the textbox, to hide the errorprovider again simply just include "" instead of putting text in the seterror. You can set the BlinkStyle of the provider to NeverBlink, AlwaysBlink and BlinkIfDifferentError.You can set those by the properties view in design time or in code in runtime. You can also change the icon at design time to whichever icon you like.

You might also like...

Comments

Colin Harman MACITP I am 26 and live in sydney, australia. I enjoy drinking and going out, but I also enjoy writing programs + creating websites in my free time. I play golf and ski. One of my main intrests whe...

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“The first 90% of the code accounts for the first 90% of the development time. The remaining 10% of the code accounts for the other 90% of the development time.” - Tom Cargill