Results 1 to 2 of 2

Thread: [2005] DateTimePicker Valuechanged event firing twice??

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2006
    Posts
    746

    [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

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    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:
    1. dtp.Value = Me.dtp_SchTaskStart.Value
    I would do this instead:

    VB.NET Code:
    1. Private Sub DateTimePicker1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker1.ValueChanged
    2.         DateTimePicker2.MinDate = DateTimePicker1.Value
    3.     End Sub
    4.  
    5.     Private Sub DateTimePicker2_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker2.ValueChanged
    6.         DateTimePicker1.MaxDate = DateTimePicker2.Value
    7.     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
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width