【发布时间】: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事件有什么建议吗?
【问题讨论】:
-
这个问题是重复的stackoverflow.com/questions/1882523/…。尝试设置 cmdCancel.CausesValidation = false; msdn.microsoft.com/en-us/library/…