《WinForm开发系列之控件篇》Item16 ErrorProvider

ErrorProvider实际上并不是一个控件,而是一个组件。当把该组件拖放到设计器上时,它会显示在设计器下方的组件栏中。当存在一个错误条件时,ErrorProvider可以在控件的旁边显示一个图标。

假定有一个TextBox控件要验证正数如果用户试图输入字符时,就必须通知用户所允许的值,需要改变输入的值。有效值的检查在文本框的Validating事件中进行。如果验证失败,就调用SetError方法,传送引起错误的控件和将该错误告知用户的字符串。然后,一个图标开始闪烁,表示出现了一个错误,用户把鼠标放在该图标上时,会显示错误文本。

图示如下

窗体编辑器:

代码
partial class Frm_ErrorProvider
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.errorProvider1 = new System.Windows.Forms.ErrorProvider(this.components);
this.label1 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
((System.ComponentModel.ISupportInitialize)(
this.errorProvider1)).BeginInit();
this.SuspendLayout();
//
// errorProvider1
//
this.errorProvider1.ContainerControl = this;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(42, 24);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(41, 12);
this.label1.TabIndex = 0;
this.label1.Text = "正数:";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(80, 20);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 21);
this.textBox1.TabIndex = 1;
this.textBox1.Validating += new System.ComponentModel.CancelEventHandler(this.textBox1_Validating);
//
// Frm_ErrorProvider
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label1);
this.Name = "Frm_ErrorProvider";
this.Text = "Frm_ErrorProvider";
((System.ComponentModel.ISupportInitialize)(
this.errorProvider1)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();

}

#endregion

private System.Windows.Forms.ErrorProvider errorProvider1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox1;
}

 

代码编辑器:

引用:using System.Text.RegularExpressions;

代码
  
public partial class Frm_ErrorProvider : Form
{
public Frm_ErrorProvider()
{
InitializeComponent();
}
/// <summary>
/// 验证控件是否输入正数
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
private bool Match(object obj)
{
TextBox tempText
= (TextBox)obj;
Regex r
= new Regex(@"^[1-9]\d*$");
return r.IsMatch(tempText.Text);
}

private void textBox1_Validating(object sender, CancelEventArgs e)
{
if (!Match(sender))
{
errorProvider1.SetError((TextBox)sender,
"请输入正整数");
e.Cancel
= true;//如果输入错误,防止输入焦点移出该控件
}
else
{
errorProvider1.SetError((TextBox)sender,
"");
e.Cancel
= false;
}

}

}

 

 

作者:Sue

出处:http://www.cnblogs.com/Sue_
文章版权归本人所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

 

posted @ 2010-01-27 11:48  Sue_娜  阅读(292)  评论(0编辑  收藏  举报