【发布时间】:2016-09-30 02:52:56
【问题描述】:
我有 2 个日期选择器。一个带有发票日期,一个带有到期日期。 我可以选择明天的发票日期。但是当我继续填写控件时,可以将截止日期留在今天。 这给出了没有触发到期事件的场景,因为我没有输入它。 现在,我不希望用户的到期日期小于实际发票日期,但由于“到期日期”事件没有被触发,我无法真正验证这一点。
谁能告诉我如何通过代码触发验证事件?
这是我目前的场景:
private void dpInvoiceDate_Validating(object sender, CancelEventArgs e)
{
// Convert the dp invoice date + hour to only date
var dateAndTime = Convert.ToDateTime(dpInvoiceDate.Text);
var date = dateAndTime.Date;
if (!InputChecks.IsGeldigeDatum(DateTime.Now.Date, Convert.ToDateTime(date)))
{
errorProvider1.SetError(dpInvoiceDate, "Invoice date can not be in the past");
e.Cancel = true;
}
else
{
errorProvider1.SetError(dpInvoiceDate, "");
}
}
private void dpDueDate_Validating(object sender, CancelEventArgs e)
{
// Convert the dp invoice date + hour to only date
var dateAndTime = Convert.ToDateTime(dpDueDate.Text);
var date = dateAndTime.Date;
var dateAndTimeInvioceDate = Convert.ToDateTime(dpInvoiceDate.Text);
var dateInvoiceDate = dateAndTimeInvioceDate.Date;
if (date < dateInvoiceDate)
{
errorProvider1.SetError(dpDueDate, "Due date can not be in the past");
e.Cancel = true;
}
else
{
errorProvider1.SetError(dpDueDate, "");
}
}
【问题讨论】:
-
我建议在您希望调用到期日期事件msdn.microsoft.com/en-us/library/… 时使用 Dispatcher.Invoke 方法
-
你在使用 Winforms 验证机制吗?
-
@progpow:验证机制到底是什么意思?在我的保存按钮中,我正在调用“(this.ValidateChildren())”,但即使单击该按钮也不会返回错误。或者至少它没有显示我的错误提供者。
-
@Needham:我无法访问 dispatcher.invoke 方法。在您提供的 microsoft 链接中,它们以“in WPF”开头。这在winforms中是否也可以,因为VS没有要求我添加参考。
标签: c# winforms events validating errorprovider