输入无效日期(必须是当前日期或过去日期)后,我有 e.Cancel = true ,但是退出按钮的关闭事件不会触发。我取出了 e.Canel = true 声明,一切似乎都运行良好,但我担心这会在未来引起另一个问题。对此进行编码的正确方法是什么?
private void maskedTextBoxDate_TypeValidationCompleted(object sender, TypeValidationEventArgs e)
{
if (!e.IsValidInput)
{
toolTip1.ToolTipTitle = "Invalid Date";
toolTip1.Show("The data you supplied must be a valid date in the format mm/dd/yyyy.", maskedTextBoxDate, 40, 25, 2000);
}
else
{
//Now that the type has passed basic type validation, enforce more specific type rules.
DateTime userDate = (DateTime)e.ReturnValue;
if (userDate > DateTime.Today)
{
toolTip1.ToolTipTitle = "Invalid Date";
toolTip1.Show("The date can't be greater than today's date.", maskedTextBoxDate, 40, 25, 2000);
//Cancel property: true if the event should be canceled; otherwise false
e.Cancel = true;
}
}
}
退出按钮关闭事件:
private void cmdExit_Click(object sender, EventArgs e)
{
this.Close();
}