Export (0) Print
Expand All

ErrorProvider.GetError Method

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.

ExceptionCondition
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();
		}
	}
The following code example demonstrates validating the TextBox1 object's text. 
It also demonstrates handling the text box’s LostFocus event by setting the file dialog's InitialDirectory
property to the text in TextBox1. The code example used the ErrorProvider.GetError method to check for an error before opening the file dialog.

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.

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft