Date Time Picker Question

00shoe

Member
Joined
Oct 12, 2006
Messages
20
Programming Experience
3-5
Hey,

I want to make a dtp that only allow users to select dates that are weekdays.

I have written the following bit of code:


Private Sub dtpPH_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtpPH.ValueChanged
Dim strDOW AsString = dtpPH.Value.DayOfWeek
If strDOW = DayOfWeek.Saturday Then
dtpPH.Value = dtpPH.Value.Subtract(System.TimeSpan.FromDays(1))
MsgBox("A Public Holiday Cannot Be On A Saturday. ", MsgBoxStyle.Exclamation, "Saturday cannot be PH")

ElseIf strDOW = DayOfWeek.Sunday Then
dtpPH.Value = dtpPH.Value.Add(System.TimeSpan.FromDays(1))
MsgBox("A Public Holiday Cannot Be On A Sunday. ", MsgBoxStyle.Exclamation, "Sunday cannot be PH")
EndIf

End Sub

However there is a problem with the code, as when a user clicks on a date that has a day of the week of saturday or sunday, the msgbox appears twice for them. (I think it’s got to do with the ValueChanged Handler).
Is there a way either:

1) Disable the user from selecting saturday or sunday.

-- or --

2) Have the msgbox only display once.

Thanks.
 
I thought forum is more for the discussion? Discussion area or Solution area?

From what previously I had read from the internet resources, the datetimepicker doesn't seems like providing any option for us to change the layout, like disabling the Saturday and Sunday. So, in my current knowledge, the option 1 seems cannot work.

For the option to display the messagebox once, let's analyst your code. In the valuechange event, you had

VB.NET:
[SIZE=2]dtpPH.Value = dtpPH.Value.Subtract(System.TimeSpan.FromDays(1))[/SIZE]

which will modify the dtpPH's value and cause it to call it's valuechange event again. If you take out the above code, it will display the msgbox only once.
 
you know, i wouldnt even bother showing the message in a messagebox - they are quite distracting to the user.. i'd just roll the day back by one if its saturday or forward by one if its sunday..

and maybe make the system beep too, possibly change a label caption to say "PH cannot be on a weekend!" - but messagebox.. mmm, too interrupting to the program flow, and not good HCI in many cases..
 
I feel that the response might be too slow if I use the Validating event, so I might try it cjard suggestion and use a label, I'm not a fan of a message box myself, but it feel for me that I need someway of telling the user that you cannot select that date (so I will try a label).

Thanks for all of your help.

Sorry if I posted this in the wrong forum area (still a newbie).
 
Back
Top