在Windows应用程序开发中,我们可以通过处理输入控件(如TextBox控件)的Validating事件,对用户的输入进行有效性验证,当用户输入不正确时,可以使用错误提示控件ErrorProvider提示用户输入错误,直至用户输入正确。如:
private void textBox1_Validating(object sender, CancelEventArgs e)
{
  try
  {
    int.Parse(textBox1.Text);
  }
  catch (Exception ex)
  {
    e.Cancel = true; // 验证没有通过
    errorProvider1.SetError(textBox1, ex.Message);
  }
}

private void textBox1_Validated(object sender, EventArgs e)
{
  errorProvider1.SetError(textBox1, "");
}
如果用户的输入验证未通过,想关闭窗体。这时会发现,无法关闭窗体。
发生这种情况的原因是,当输入焦点已经在这个输入控件时,如果你去点取消或关闭按钮,会使得该控件失去焦点,从而引发该控件的Validating事件。由于CancelEventArgs的Cancel属性为true, 所以焦点始终无法离开,无法关闭这个窗体了。
我们可以改用控件的Leave事件来进行验证。
private void textBox1_Leave(object sender, EventArgs e)
{
  try
  {
    int.Parse(textBox1.Text);
  }
  catch (Exception ex)
  {
    textBox1.Focus();
    errorProvider1.SetError(textBox1, ex.Message);
  }
}
private void textBox1_Validated(object sender, EventArgs e)
{
  errorProvider1.SetError(textBox1, "");
}

ErrorProvider控件使用的更多相关文章

  1. Winform ErrorProvider控件使用

    要实现的功能:判断第一个文本框中输入的是不是字符 “a”. 最终效果: *当输入的不是a,控件旁会显示错误图标.当输入的是a,则错误图标会消失. 首先添加ErrorProvider控件. 代码: pr ...

  2. 错误提示控件errorProvider

    http://www.cnblogs.com/suguoqiang/archive/2012/07/17/2596564.html 错误提示控件errorProvider VS显示: 核心代码: th ...

  3. C#的winform控件命名规范

    注:这里用红字标记的部分表示有重复出现,括号内为替代表示方案 1.标准控件 序号 控件类型简写 控件类型 1 btn Button 2 chk CheckBox 3 ckl CheckedListBo ...

  4. ASP.NET 控件前缀命名规范

    标准控件 1  btn Button 2  chk CheckBox 3  ckl CheckedListBox 4  cmb ComboBox 5  dtp DateTimePicker 6  lb ...

  5. (转)WPF控件开源资源

    (转)WPF控件开源资源 Textbox Drag/Drop in WPFhttp://www.codeproject.com/Articles/42696/Textbox-Drag-Drop-in- ...

  6. C#winfrom控件命名规范

     ※用红字标记的部分表示有重复出现,括号内为替代表示方案 1.标准控件 序号 控件类型简写 控件类型 1 btn Button 2 chk CheckBox 3 ckl CheckedListBox ...

  7. C# 控件缩写大全+命名规范+示例

    如有转载,请注明出处:http://www.cnblogs.com/flydoos/archive/2011/08/29/2158903.html C# 控件缩写大全+命名规范+示例 写程序的时候突然 ...

  8. C#控件前缀命名规范

    标准控件 1  btn Button 2  chk CheckBox 3  ckl CheckedListBox 4  cmb ComboBox 5  dtp DateTimePicker 6  lb ...

  9. WinForm控件使用文章收藏整理完成

    对C# WinForm开发系列收集的控件使用方面进行整理, 加入了一些文章, 不断补充充实, 完善这方面. 基础 - 常用控件 C# WinForm开发系列 - CheckBox/Button/Lab ...

随机推荐

  1. Android 开发之修改 app 的字体大小(老人模式)

    新的需求(可参见 微信和QQ改变字体): app 字体不随着系统字体大小变化 app 设置中有设置字体大小的开关,变大以后,整个 app 字体变大. 解决方案:(字体需要采用 dp 为单位,不能使用 ...

  2. wget常见用法

    1.很多软件官网会有安装脚本,并把脚本搞成raw模式,方便下载后直接运行的shell文件.比如docker wget -qO- get.docker.com | bash -q的含义是:--quiet ...

  3. [100]find&xargs命令

    打算把基础命令常用选项做个总结. find命令参数 - 命令格式 find . -type f -name '*.txt' - 命令参数 find #查找文件 -type #指定类型 f 文件 d 目 ...

  4. 用Duplex实现消息广播

    WCF中定义3种消息交换模式: 1. Request/Reply; 2. One-Way; 3. Duplex.  Request/Reply 是缺省模式,即同步调用.在调用服务方法后需要等待服务的消 ...

  5. flink-jdbc sink

    https://github.com/apache/flink/tree/master/flink-connectors/flink-jdbc/src https://blog.csdn.net/lu ...

  6. HHH

    https://data-artisans.com/flink-forward/resources/alibabas-common-algorithm-platform-on-flink https: ...

  7. artificial%20intelligence%20a%20modern%20approach

    http://stpk.cs.rtu.lv/sites/all/files/stpk/materiali/mi/artificial%20intelligence%20a%20modern%20app ...

  8. Ctex中WinEdt经常弹出注册小窗口 解决办法

    使用WinEdt 7避免跳出“注册对话框” 在options菜单下点options…,在advanced configuration  =>  Event Handlers  下点Exit, 在 ...

  9. 每日英语:Got a Case of the Mondays? Blame the Sunday Blues

    Welcome to Monday morning at the office. Did you have trouble sleeping last night? Was your stomach ...

  10. linux命令(42):tr命令

    Linux tr命令 Linux tr 命令用于转换或删除文件中的字符. tr 指令从标准输入设备读取数据,经过字符串转译后,将结果输出到标准输出设备. 语法: tr [-cdst][--help][ ...