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

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

《WinForm开发系列之控件篇》Item16 ErrorProvider相关推荐

  1. 《WinForm开发系列之控件篇》Item2 BindingNavigator

    WinForm之中BindingNavigator控件的使用 在微软WinForm中,BindingNavigator控件主要用来绑定数据.可以将一个数据集合与该控件绑定,以进行数据 联动的显示效果 ...

  2. 《WinForm开发系列之控件篇》Item1 BackgroungWorker

    cranejuan的专栏 BackgroundWorker实现原理 winfom組件---BackgroundWorker 转载于:https://www.cnblogs.com/Sue_/artic ...

  3. 《WinForm开发系列之控件篇》Item18 FileSystemWatcher(暂无)

    暂无 转载于:https://www.cnblogs.com/Sue_/articles/1657381.html

  4. 《WinForm开发系列之控件篇》Item28 LinkView(暂无)

    暂无 转载于:https://www.cnblogs.com/Sue_/articles/1657443.html

  5. 《WinForm开发系列之控件篇》Item22 HelpProvider(暂无)

    暂无 转载于:https://www.cnblogs.com/Sue_/articles/1657390.html

  6. 《WinForm开发系列之控件篇》Item33 NotifyIcon(暂无)

    暂无 转载于:https://www.cnblogs.com/Sue_/articles/1657452.html

  7. 《WinForm开发系列之控件篇》Item13 DirectoryEntry(暂无)

    暂无 转载于:https://www.cnblogs.com/Sue_/articles/1657367.html

  8. 《WinForm开发系列之控件篇》Item25 Lable(暂无)

    暂无 转载于:https://www.cnblogs.com/Sue_/articles/1657440.html

  9. 《WinForm开发系列之控件篇》Item3 BindingSource (暂无)

    暂无 转载于:https://www.cnblogs.com/Sue_/articles/1657348.html

  10. 《WinForm开发系列之控件篇》Item31 MenuStrip(暂无)

    暂无 转载于:https://www.cnblogs.com/Sue_/articles/1657446.html

最新文章

  1. android native java_在Android Native层中创建Java虚拟机实例
  2. linux ipmitool检测内存,一种基于ipmitool工具循环侦测内存的方法与流程
  3. Python爬虫实战(4):抓取淘宝MM照片
  4. vs2013 error MSB8031 MBCSMFC问题的解决
  5. Word Embedding News | 词嵌入新鲜事:COVID-19特刊
  6. android 图片 切换,Android 应用开发笔记 - 切换图片(ImageSwitcher)
  7. 【Java】异常处理的注意事项
  8. 1180魔方阵(每日学习)宁波大学OJ
  9. 计算机车牌识别的步骤,车牌识别流程图
  10. linux用户权限不够解析及解决方案
  11. 中职一年级计算机学情分析,一年级学情分析.doc
  12. devise rails
  13. 安全港到隐私护盾!美欧个人数据跨境流动20年政策变迁
  14. python自动化操作浏览器
  15. 计算机管理服务修复,电脑提示“部署映像服务和管理工具错误87”的修复步骤...
  16. squid配置透明代理并支持Https及http、https拦截
  17. java快速生成数据库文档
  18. 总结了下PHPExcel官方读取的几个例子
  19. matlab中的sin(函数)
  20. C盘临时文件怎么删除?

热门文章

  1. [JAVA]预面试笔记
  2. JavaScript的作用域详解
  3. JNDI配置数据库连接
  4. [原创]测试用例设计之“功能图”法
  5. 每天00:00,MySQL定时弹出一个taskeng.exe
  6. 网传字节跳动实习生删除GB以下所有机器学习模型,差点没上头条......
  7. 超全面的权限系统设计方案!(万能通用)
  8. 为什么谷歌要执行严格的代码编写规范?
  9. 如何优雅地根治null值引起的Bug?!
  10. 恕我直言,你完全没有把IDEA的Diagram功能发挥出来...