Click here to Skip to main content
15,887,214 members
Articles / Programming Languages / C#

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  
How to use error provider control in Windows Form application
/*
 * Created by SharpDevelop.
 * User: Administrator
 * Date: 9/21/2013
 * Time: 5:04 PM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace Error_Provider
{
	/// <summary>
	/// Description of MainForm.
	/// </summary>
	public partial class MainForm : Form
	{
		public MainForm()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
		}
		
		
		
		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,"");
				}
			}
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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