當前位置: 首頁>>代碼示例>>C#>>正文


C# ErrorProvider.GetError方法代碼示例

本文整理匯總了C#中System.Windows.Forms.ErrorProvider.GetError方法的典型用法代碼示例。如果您正苦於以下問題:C# ErrorProvider.GetError方法的具體用法?C# ErrorProvider.GetError怎麽用?C# ErrorProvider.GetError使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在System.Windows.Forms.ErrorProvider的用法示例。


在下文中一共展示了ErrorProvider.GetError方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: textBox1_Validating

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();
    }
}
開發者ID:.NET開發者,項目名稱:System.Windows.Forms,代碼行數:42,代碼來源:ErrorProvider.GetError

示例2: TextBoxValidationRegex

//引入命名空間
using System;
using System.Windows.Forms;
using System.Text.RegularExpressions;

public class TextBoxValidationRegex : Form
{
    public TextBoxValidationRegex()
    {
        this.components = new System.ComponentModel.Container();
        this.Button1 = new System.Windows.Forms.Button();
        this.errProvider = new System.Windows.Forms.ErrorProvider(this.components);
        this.Label3 = new System.Windows.Forms.Label();
        this.txtEmail = new System.Windows.Forms.TextBox();
        ((System.ComponentModel.ISupportInitialize)(this.errProvider)).BeginInit();
        this.SuspendLayout();

        this.Button1.Location = new System.Drawing.Point(212, 80);
        this.Button1.Name = "Button1";
        this.Button1.Size = new System.Drawing.Size(76, 24);
        this.Button1.TabIndex = 12;
        this.Button1.Text = "OK";
        this.Button1.Click += new System.EventHandler(this.Button1_Click);

        this.errProvider.ContainerControl = this;
        this.errProvider.DataMember = "";

        this.Label3.Location = new System.Drawing.Point(24, 24);
        this.Label3.Size = new System.Drawing.Size(40, 16);
        this.Label3.Text = "Email:";

        this.txtEmail.Location = new System.Drawing.Point(68, 20);
        this.txtEmail.Size = new System.Drawing.Size(220, 21);
        this.txtEmail.Leave += new System.EventHandler(this.txtEmail_Leave);

        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(328, 126);
        this.Controls.Add(this.Label3);
        this.Controls.Add(this.txtEmail);
        this.Controls.Add(this.Button1);
        this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        ((System.ComponentModel.ISupportInitialize)(this.errProvider)).EndInit();
        this.ResumeLayout(false);
        this.PerformLayout();

    }

    private void Button1_Click(object sender, EventArgs e)
    {
        string errorText = "";

        foreach (Control ctrl in this.Controls)
        {
            if (errProvider.GetError(ctrl) != "")
            {
                errorText += "   * " + errProvider.GetError(ctrl) + "\n";
                
            }
            MessageBox.Show("Errors:" +errorText, "Invalid Input", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
    }

    private void txtEmail_Leave(object sender, EventArgs e)
    {
        Regex regex;
        regex = new Regex(@"^[\w-]+@([\w-]+\.)+[\w-]+$");

        Control ctrl = (Control)sender;
        if (regex.IsMatch(ctrl.Text) || ctrl.Text == "")
        {
            errProvider.SetError(ctrl, "");
        }
        else
        {
            errProvider.SetError(ctrl, "This is not a valid email address.");
        }
    }

    [STAThread]
    public static void Main(string[] args)
    {
        Application.Run(new TextBoxValidationRegex());
    }
    private System.Windows.Forms.Button Button1;
    private System.Windows.Forms.ErrorProvider errProvider;
    private System.Windows.Forms.Label Label3;
    private System.Windows.Forms.TextBox txtEmail;

    private System.ComponentModel.IContainer components = null;

}
開發者ID:C#程序員,項目名稱:System.Windows.Forms,代碼行數:92,代碼來源:ErrorProvider.GetError


注:本文中的System.Windows.Forms.ErrorProvider.GetError方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。