zoukankan      html  css  js  c++  java
  • ErrorProvider控件使用

    在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, "");
    }

  • 相关阅读:
    模拟105 题解
    模拟104 题解
    模拟103 题解
    模拟102 题解
    python与 Ajax跨域请求
    Django--进阶--中间件的使用
    Django--权限组件
    python 最基本的的单例模型的实现及应用
    Django-数据库访问优化
    Create views of OpenCASCADE objects in the Debugger
  • 原文地址:https://www.cnblogs.com/zhouhb/p/4357487.html
Copyright © 2011-2022 走看看