【问题标题】:In a textbox validating event, how to ignore clicks to a certain button?在文本框验证事件中,如何忽略对某个按钮的点击?
【发布时间】:2014-03-06 00:55:22
【问题描述】:

问题是这样的:

我有一个接受 TextBox 文本和 2 个按钮的表单,一个用于将文本保存到数据库中,另一个用于取消和关闭表单。该表单还有一个在 TextBox.Validating 事件中使用的 ErrorProvider,如下所示:

private void txtNombre_Validating(object sender, CancelEventArgs e)
{
    string errorMsg;
    if (!ValidateText(txtNombre.Text, out errorMsg))
    {
        // Cancel the event and select the text to be corrected by the user.
        e.Cancel = true;
        txtNombre.Select(0, txtNombre.Text.Length);

        // Set the ErrorProvider error with the text to display.  
        this.errorProvider1.SetError(txtNombre, errorMsg);
    }
}

它符合预期,在填写必填字段之前不允许用户在表单中执行任何操作,除了一件事:让用户通过“取消”按钮关闭表单。 我尝试将 object sender 与按钮 cmdCancel 进行比较,但这不可能,因为发件人始终是 TextBox。

关于当用户点击cmdCancel时如何忽略Validating事件有什么建议吗?

【问题讨论】:

标签: c# winforms


【解决方案1】:

查看属性Control.CausesValidation。为您的 cancel 按钮将其设置为 false。

【讨论】:

    【解决方案2】:

    在“取消”按钮上将 CausesValidation 设置为 false。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-19
      • 2017-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多