Introduction
Today, I have provided an article showing you how to use the ErrorProvider control in a Windows Forms application. The ErrorProvider control provides a user interface to indicate to the user that a control on a form has an error associated with it, or the ErrorProvider component provides an easy way to set validation errors. Here, you will see a demo of the Errorprovider control with a ToolTip. When an error message is set, an icon indicating the error will appear next to the control and the error message is displayed as a ToolTip when the mouse is over the control. All you have to do is implement and hook it up to your application. First of all you create a Windows Forms application in Visual Studio 2010.
Now you have to create a Windows Forms application.
- Go to Visual Studio 2010
- New-> Select a project
- Click OK
Now add a new page to the Windows Forms application.
- Go to the Solution Explorer
- Right-click on the Project name
- Select add new item
- Add new windows Form and give it a name
- Click OK
First of all drag the TextBoxes control, Button and ErrorProvider control from the Toolbox on the form. The form looks like this:
Now select the ErrorProvider control and press F4 to set the the following property of the ErrorProvider control:
- Icon - Icon property are used to indicate an error.
- BlinkStyle - Controls whether the error icon blinks when an error is set.
Now suppose you do not write any text in the TextBoxes on the form and click on the Button; an icon will be displayed to the TextBoxes that show an error and the specified text will appear in the ToolTip box when the mouse is over the control. The code for that looks like this:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication4
{
public partial class ErrorProvider : Form
{
public ErrorProvider()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (textBoxName.Text == "")
{
errorProvider1.SetError(textBoxName, "Please enter the Name");
}
if (textBoxCountry.Text == "")
{
errorProvider1.SetError(textBoxCountry, "Please enter the Country");
}
if (textBoxCity.Text == "")
{
errorProvider1.SetError(textBoxCity, "Please enter the City");
}
if (textBoxPnumber.Text == "")
{
errorProvider1.SetError(textBoxPnumber, "Please enter the Phone Number");
}
}
}
}
SetError Method - The SetError method is used to display an error string message on the specified control. Now move the mouse over the SetError Method. The following image defines the method.
Now run the application and click on the Button without entering any text in the TextBoxes. It displays the tool tip and error icon.
Now move the mouse over the error icon. The error message will be display as a ToolTip.
Now write the name and country on the TextBoxes and click on the Button. It displays the error icon.
Now write some text in the TextBoxes and click on the Button. It does not display the tool tip and error icon.