Csharp/CSharp Tutorial/GUI Windows Forms/ErrorProvider

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

ErrorProvider: number must be in a range

using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    private void numberBox_Validating(object sender, CancelEventArgs e)
    {
        try
        {
            int numberEntered = int.Parse(numberBox.Text);
            if (numberEntered < 1 || numberEntered > 10)
            {
                e.Cancel = true;
                errorProvider1.SetError(numberBox, "You must enter a number between 1 and 10");
            }
        }
        catch (FormatException)
        {
            e.Cancel = true;
            errorProvider1.SetError(numberBox, "You need to enter a whole number");
        }
    }
    private void numberBox_Validated(object sender, EventArgs e)
    {
        MessageBox.Show("Well done, you managed to enter a valid number");
    }
    private void okButton_Click(object sender, EventArgs e)
    {
        this.Close();
    }
    private void numberBox_TextChanged(object sender, EventArgs e)
    {
        MessageBox.Show("text changed");
    }
}
partial class Form1
{
    private void InitializeComponent()
    {
        System.ruponentModel.Container components = new System.ruponentModel.Container();
        this.numberBox = new System.Windows.Forms.TextBox();
        this.label1 = new System.Windows.Forms.Label();
        this.okButton = new System.Windows.Forms.Button();
        this.errorProvider1 = new System.Windows.Forms.ErrorProvider(components);
        ((System.ruponentModel.ISupportInitialize)(this.errorProvider1)).BeginInit();
        this.SuspendLayout();
        // 
        // numberBox
        // 
        this.numberBox.Location = new System.Drawing.Point(190, 12);
        this.numberBox.Name = "numberBox";
        this.numberBox.Size = new System.Drawing.Size(44, 20);
        this.numberBox.TabIndex = 0;
        this.numberBox.Validated += new System.EventHandler(this.numberBox_Validated);
        this.numberBox.Validating += new System.ruponentModel.CancelEventHandler(this.numberBox_Validating);
        this.numberBox.TextChanged += new System.EventHandler(this.numberBox_TextChanged);
        // 
        // label1
        // 
        this.label1.AutoSize = true;
        this.label1.Location = new System.Drawing.Point(12, 15);
        this.label1.Name = "label1";
        this.label1.Size = new System.Drawing.Size(164, 13);
        this.label1.TabIndex = 1;
        this.label1.Text = "Enter a number between 1 and 10";
        // 
        // okButton
        // 
        this.okButton.Location = new System.Drawing.Point(251, 9);
        this.okButton.Name = "okButton";
        this.okButton.Size = new System.Drawing.Size(75, 23);
        this.okButton.TabIndex = 2;
        this.okButton.Text = "OK";
        this.okButton.Click += new System.EventHandler(this.okButton_Click);
        // 
        // errorProvider1
        // 
        this.errorProvider1.ContainerControl = this;
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(340, 47);
        this.Controls.Add(this.okButton);
        this.Controls.Add(this.label1);
        this.Controls.Add(this.numberBox);
        this.Name = "Form1";
        this.Text = "Form1";
        ((System.ruponentModel.ISupportInitialize)(this.errorProvider1)).EndInit();
        this.ResumeLayout(false);
        this.PerformLayout();
    }
    private System.Windows.Forms.TextBox numberBox;
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.Button okButton;
    private System.Windows.Forms.ErrorProvider errorProvider1;
}
public class ErrorProviderTextBox
{
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.Run(new Form1());
    }
}

ErrorProvider: Text length

using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class ErrorForm : System.Windows.Forms.Form
{
  private System.ruponentModel.Container components = null;
  private System.Windows.Forms.Label label1;
  private System.Windows.Forms.Button btnValidate;
  private System.Windows.Forms.ErrorProvider errorProvider1;
  private System.Windows.Forms.TextBox txtInput;
  public ErrorForm()
  {
    InitializeComponent();
  }
  protected override void Dispose( bool disposing )
  {
    if( disposing )
    {
      if (components != null) 
      {
        components.Dispose();
      }
    }
    base.Dispose( disposing );
  }
  private void InitializeComponent()
  {
    this.errorProvider1 = new System.Windows.Forms.ErrorProvider();
    this.label1 = new System.Windows.Forms.Label();
    this.txtInput = new System.Windows.Forms.TextBox();
    this.btnValidate = new System.Windows.Forms.Button();
    this.SuspendLayout();
    // 
    // errorProvider1
    // 
    this.errorProvider1.BlinkRate = 500;
    this.errorProvider1.BlinkStyle = System.Windows.Forms.ErrorBlinkStyle.AlwaysBlink;
    this.errorProvider1.ContainerControl = this;
    // 
    // label1
    // 
    this.label1.Font = new System.Drawing.Font("Arial Black", 12F);
    this.label1.Location = new System.Drawing.Point(8, 8);
    this.label1.Name = "label1";
    this.label1.Size = new System.Drawing.Size(376, 56);
    this.label1.TabIndex = 2;
    this.label1.Text = "The following text box only allows 5 characters.  Try to enter more...";
    // 
    // txtInput
    // 
    this.txtInput.Location = new System.Drawing.Point(144, 80);
    this.txtInput.Name = "txtInput";
    this.txtInput.Size = new System.Drawing.Size(120, 20);
    this.txtInput.TabIndex = 0;
    this.txtInput.Text = "";
    this.txtInput.Validating += new System.ruponentModel.CancelEventHandler(this.txtInput_Validating);
    // 
    // btnValidate
    // 
    this.btnValidate.Location = new System.Drawing.Point(16, 72);
    this.btnValidate.Name = "btnValidate";
    this.btnValidate.Size = new System.Drawing.Size(112, 32);
    this.btnValidate.TabIndex = 1;
    this.btnValidate.Text = "OK";
    // 
    // ErrorForm
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.ClientSize = new System.Drawing.Size(400, 125);
    this.Controls.Add(this.label1);
    this.Controls.Add(this.btnValidate);
    this.Controls.Add(this.txtInput);
    this.Name = "ErrorForm";
    this.Text = "Error Trapper";
    this.ResumeLayout(false);
  }
  [STAThread]
  static void Main() 
  {
    Application.Run(new ErrorForm());
  }
  private void txtInput_Validating(object sender, System.ruponentModel.CancelEventArgs e)
  {
    // Check if the text length is greater than 5.
    if(txtInput.Text.Length > 5)
    {
      errorProvider1.SetError( txtInput, "Can"t be greater than 5!");
    } 
    else
      errorProvider1.SetError(txtInput, "");
  }
}

Get ErrorMessage from ErrorProvider

using System;
using System.Windows.Forms;
using System.Text.RegularExpressions;
public class TextBoxValidationRegex : Form
{
    public TextBoxValidationRegex()
    {
        this.ruponents = new System.ruponentModel.Container();
        this.Button1 = new System.Windows.Forms.Button();
        this.errProvider = new System.Windows.Forms.ErrorProvider(this.ruponents);
        this.Label3 = new System.Windows.Forms.Label();
        this.txtEmail = new System.Windows.Forms.TextBox();
        ((System.ruponentModel.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.ruponentModel.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.ruponentModel.IContainer components = null;
}