[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2634
  • Last Modified:

DateTimePicker validating event unreliable

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.
But....
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?

Brian


0
bflow
Asked:
bflow
1 Solution
 
Priest04Commented:
I cant manifest this behaviour....

private void dateTimePicker1_Validating(object sender, CancelEventArgs e)
        {
            MessageBox.Show(dateTimePicker1.Value.ToString());
        }


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

Goran
0
 
RubenvdLindenCommented:
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.
0
 
bflowAuthor Commented:
Great!

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

Thanks a lot.

Brian
0
 
udhayakumardCommented:
Hi,

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");
 }
0

Featured Post

Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

Tackle projects and never again get stuck behind a technical roadblock.
Join Now