ErrorProvider.GetError Method (Control)

 

Returns the current error description string for the specified control.

Namespace:   System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public string GetError(
	Control control
)

Parameters

control
Type: System.Windows.Forms.Control

The item to get the error description string for.

Return Value

Type: System.String

The error description string for the specified control.

Exception Condition
ArgumentNullException

control is null.

The following code example uses the GetError method to check for an error before opening a file dialog box. To run this example, paste the following code into a form containing a TextBox named TextBox1, an OpenFileDialog named OpenFileDialog1, a button named Button1, and an ErrorProvider named ErrorProvider1. Ensure all events are associated with their event handlers.

private void textBox1_Validating(object sender, 
	System.ComponentModel.CancelEventArgs e)
{
	// If nothing is entered,
	// an ArgumentException is caught; if an invalid directory is entered, 
	// a DirectoryNotFoundException is caught. An appropriate error message 
	// is displayed in either case.
	try
	{
		System.IO.DirectoryInfo directory = 
			new System.IO.DirectoryInfo(textBox1.Text);
		directory.GetFiles();
		errorProvider1.SetError(textBox1, "");

	}
	catch(System.ArgumentException ex1)
	{
		errorProvider1.SetError(textBox1, "Please enter a directory");

	}
	catch(System.IO.DirectoryNotFoundException ex2)
	{
		errorProvider1.SetError(textBox1, "The directory does not exist." +
			"Try again with a different directory.");
	}

}

// This method handles the LostFocus event for textBox1 by setting the 
// dialog's InitialDirectory property to the text in textBox1.
private void textBox1_LostFocus(object sender, System.EventArgs e)
{
	openFileDialog1.InitialDirectory = textBox1.Text;
}

// This method demonstrates using the ErrorProvider.GetError method 
// to check for an error before opening the dialog box.
private void button1_Click(System.Object sender, System.EventArgs e)
{
	//If there is no error, then open the dialog box.
	if (errorProvider1.GetError(textBox1)=="")
	{
		DialogResult dialogResult = openFileDialog1.ShowDialog();
	}
}

.NET Framework
Available since 1.1
Return to top
Show: