DateTimePicker validating event unreliable

Posted on 2007-08-06
Last Modified: 2013-11-07
I have a couple of datetimepicker fields on my form.
I need to simply validate if one date is less than the other.
Therefore on the validating event of the 2nd field I perform a check:
if Date2.Value < Date1.Value

This works fine if I the validation is as a result of a selection from the datetimepicker lookup.
if the date is showing 05/06/2007 for example and the user enters 04/06/2007 i.e. just changes the 5 to 4 and tabs out of the field the value is still showing 05/06/2007.
If I then go back in to the field and change it again the value will then show 05/06/2007.
i.e. it's out of step.

Any ideas why this is happening and how to resolve it?


Question by:bflow
    LVL 18

    Expert Comment

    I cant manifest this behaviour....

    private void dateTimePicker1_Validating(object sender, CancelEventArgs e)

    it will always show the same value as in DateTimePicker1, no matter if I select value, or enter if manually, or change it with KeyUp/Down

    LVL 12

    Accepted Solution

    I can reproduce this behaviour; it's very annoying.
    It seems that the Change event is fired delayed, causing the Validating event to fire first (validating the old value instead of the new value).

    As a workaround, you can add these lines to the beginning of your validating event to 'force' an update of the value before validation takes place:

    private void Date2_Validating (object sender, CancelEventArgs e)
        // Workaround code
        Date2.Visible = false;
        Date2.Visible = true;

        // Your validation code here

    I hope this helps.

    Author Comment


    Saved a lot of time, making the field invisible and then visible again did the trick!

    Thanks a lot.

    LVL 11

    Expert Comment


    Use the ValueChanged event instead of Validating

    this.dateTimePicker1.ValueChanged += new System.EventHandler(this.dateTimePicker1_ValueChanged);

     private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
                MessageBox.Show("Value Changed");

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Join & Write a Comment

    Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
    Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    This video discusses moving either the default database or any database to a new volume.

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now