ErrorProvider.GetError(Control) Yöntem

Tanım

Belirtilen denetim için geçerli hata açıklama dizesini döndürür.

C#
public string GetError (System.Windows.Forms.Control control);

Parametreler

control
Control

Hata açıklama dizesini almak için öğe.

Döndürülenler

String

Belirtilen denetim için hata açıklaması dizesi.

Özel durumlar

control, null değeridir.

Örnekler

Aşağıdaki kod örneği, dosya iletişim kutusunu açmadan önce hata olup olmadığını denetlemek için yöntemini kullanır GetError . Bu örneği çalıştırmak için, aşağıdaki kodu adlı , adlı TextBox1, adlı OpenFileDialog1bir OpenFileDialog düğmeyi Button1ve adlı bir öğesini ErrorProvider1içeren TextBox bir ErrorProvider forma yapıştırın. Tüm olayların olay işleyicileriyle ilişkilendirildiğinden emin olun.

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

Şunlara uygulanır

Ürün Sürümler
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
Windows Desktop 3.0, 3.1, 5, 6, 7

Ayrıca bkz.