【问题标题】:simple user input validation in winforms [duplicate]winforms中的简单用户输入验证[重复]
【发布时间】:2013-07-08 06:14:14
【问题描述】:

我想在我的表单中实现对用户输入的简单验证。

我有要验证的 errorProvider1 和 txtCode 表单字段。所以我放了以下

private void txtCode_Validating(object sender, CancelEventArgs e)
        {
            if (txtCode.Text == "")
            {
                e.Cancel = true;
                errorProvider1.SetError(txtCode, "Field cannot be empty");
            }
            else
            {
                errorProvider1.SetError(txtCode,"");
            }

        }

我不知道当用户点击确定按钮时如何调用txtCode_Validating这个方法?

【问题讨论】:

    标签: .net winforms


    【解决方案1】:

    你可以使用这样的东西

    private void btnOK_Click(object sender, System.EventArgs e)
    {
       foreach (Control control in this.Controls)
       {
        // Set focus on control
        control.Focus();
        // Validate causes the control's Validating event to be fired,
        // if CausesValidation is True
        if (!Validate())
        {
            DialogResult = DialogResult.None;
            return;
        }
       }
    }
    

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-03-06
      • 1970-01-01
      • 2012-07-22
      • 1970-01-01
      • 1970-01-01
      • 2011-03-25
      • 2017-09-14
      相关资源
      最近更新 更多