在文本框验证事件中,如何忽略对某个按钮的点击?

5 c# winforms

问题是这样的:

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

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

它做了预期的事情,不要让用户在填写必填字段之前在表单中做任何事情,除了一件事:让用户通过Cancel按钮关闭表单.我尝试将object sender按钮与按钮进行比较,cmdCancel但这是不可能的,因为发送方始终是TextBox.

关于如何在用户点击时忽略Validating事件的任何建议cmdCancel

Nic*_*rey 3

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