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)

[LocalizableAttribute(true)] 
public string GetError (
	Control control
)
/** @attribute LocalizableAttribute(true) */ 
public String GetError (
	Control control
)
LocalizableAttribute(true) 
public function GetError (
	control : Control
) : String
Not applicable.

Parameters

control

The item to get the error description string for.

Return Value

The error description string for the specified control.

Exception typeCondition

ArgumentNullException

control is a null reference (Nothing in Visual Basic).

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();
    }
}

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.get_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.");
    }
} //textBox1_Validating

// 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.set_InitialDirectory(textBox1.get_Text());
} //textBox1_LostFocus

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

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show: