.NET How To Use ErrorProvider For Windows Form
Posted by tech on
January 16, 2009
Ever want to do some error notification in your application using .NET that looks like this?
Using an ErrorProvider object, you can. In VIsual Studio, just drag the ErrorProvider from the list of components to your container form. There should be only one ErrorProvider per form. I do not see any reason why you need to have 2 or more.
Supposing you have a TextBox named textBox1 and an ErrorProvider object named errorProvider1. You could place this code inside your class constructor
this.textBox1.Validating += new System.ComponentModel.CancelEventHandler(this.textBox1_Validating);
add this method within your class.
private void textBox1_Validating(object sender, System.ComponentModel.CancelEventArgs e) { if (textBox1.Text == "") { errorProvider1.SetError (textBox1, "Please enter your Name"); } else { errorProvider1.SetError(textBox1, ""); } }
This code activates the validation when the TextBox object loses focus. If for example you wish to execute validation when the LOGIN button is clicked, you can use the ValidateChild() method of the container class.







