|
-
May 16th, 2008, 08:55 AM
#1
Thread Starter
Fanatic Member
[2005] DateTimePicker Valuechanged event firing twice??
This seems pretty straighforward. I have two DateTimePickers. One for a start date and the other for an end date.
I want to check when each one has changed to ensure that the dates are consistent (ie. end date is greater than start date).
This is the code I have so far.
Code:
Private Sub dtp_SchTaskEnd_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtp_SchTaskEnd.ValueChanged
Dim dtp As DateTimePicker = DirectCast(sender, DateTimePicker)
If dtp.Value.ToShortDateString < Me.dtp_SchTaskStart.Value.ToShortDateString Then
MessageBox.Show("End date cannot be earlier than start date", "Wrong end date", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
dtp.Value = Me.dtp_SchTaskStart.Value
End If
End Sub
However, the warning message gets shown twice if the If condition is true. Why is that?
ManagePC - the all-in-one PC management and inventory tool
-
May 16th, 2008, 08:59 AM
#2
Re: [2005] DateTimePicker Valuechanged event firing twice??
It would probably be because you are setting the value of the datetimepicker inside the eventhandler:
VB.NET Code:
dtp.Value = Me.dtp_SchTaskStart.Value
I would do this instead:
VB.NET Code:
Private Sub DateTimePicker1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker1.ValueChanged
DateTimePicker2.MinDate = DateTimePicker1.Value
End Sub
Private Sub DateTimePicker2_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker2.ValueChanged
DateTimePicker1.MaxDate = DateTimePicker2.Value
End Sub
DateTimePicker1 can never have a date higher than the date in DateTimePicker2, and DateTimePicker2 can never have a date lower than the date in DateTimePicker1.
No need for error messages
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|