简体   繁体   中英

Need help on this logic… (.NET)

This is the code I have in my form to check if the date the user selected is more than 14 days in advance, or in the past.

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

It works, but if I select a date more than 14 days ahead it won't show the error message, because of the second IF checking if it is in the past and blanking it.

I really can't think of another way around this other than making another textbox to sit behind the one the user types into, and displaying the second error message to that one.

Anybody have any bright ideas? Thanks :)

try this

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

You are very close! Just place the check in a else if block.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM