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

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

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

图示如下

2010012714382447.jpg

窗体编辑器:

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
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;

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
  
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_
文章版权归本人所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

 

转载于:https://www.cnblogs.com/Sue_/articles/1657376.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值