1

这是我在表单中的代码,用于检查用户选择的日期是提前 14 天还是过去。

If (dtpDate.Value > DateTime.Today.AddDays(14)) Then
    frmBookErr.SetError(dtpDate, "You cannot book more than two weeks in advance.")
Else
    frmBookErr.SetError(dtpDate, "")
End If
If (dtpDate.Value < DateTime.Today) Then
    frmBookErr.SetError(dtpDate, "You cannot book a room for the past.")
Else
    frmBookErr.SetError(dtpDate, "")
End If

它可以工作,但如果我选择一个超过 14 天的日期,它不会显示错误消息,因为第二个 IF 检查它是否在过去并将其消隐。

除了制作另一个文本框位于用户键入的文本框的后面并向该文本框显示第二条错误消息之外,我真的想不出另一种解决方法。

有人有什么好主意吗?谢谢 :)

4

2 回答 2

8

尝试这个

If (dtpDate.Value > DateTime.Today.AddDays(14)) Then
    frmBookErr.SetError(dtpDate, "You cannot book more than two weeks in advance.")
Else If (dtpDate.Value < DateTime.Today) Then
    frmBookErr.SetError(dtpDate, "You cannot book a room for the past.")
Else
    frmBookErr.SetError(dtpDate, "")
End If
于 2009-10-29T18:48:11.737 回答
1

你很亲近!只需将检查放在 else if 块中。

If (dtpDate.Value > DateTime.Today.AddDays(14)) Then
    frmBookErr.SetError(dtpDate, "You cannot book more than two weeks in advance.")
Else If (dtpDate.Value < DateTime.Today) Then
    frmBookErr.SetError(dtpDate, "You cannot book a room for the past.")
Else
    frmBookErr.SetError(dtpDate, "")
End If
于 2009-10-29T18:50:27.737 回答