Avatar of Member_2_4619940
Member_2_4619940
 asked on

C# datetime validation problem

We have a Visual Studio 2010, C#, Windows application.

We have a custom form in the C# project which has a datetime control.
We are capturing the Validating event for this control.

The event appears to get fired correctly when the user attempts to leave the control.

If the user uses the mouse to select a new date, when inside the validation routine, the Value property is set to the newly selected value.

However, if the user uses the keyboard to enter a new date, when inside the validation routine, the Value property is still set to the previous value.

How do I get the Value property to have the correct value after the user changes the value with the keyboard? Or, what is the correct way to get the new value when inside the Validating event for the datetime control?

Thanks.
C#Microsoft Development

Avatar of undefined
Last Comment
it_saige

8/22/2022 - Mon
plusone3055

can you post the validation method ?
Member_2_4619940

ASKER
Sure. I've been able to recreate this in a new Windows project.
The validation snippet from the test project:

private void dtReqShipDate_Validating(object sender, CancelEventArgs e)
{
      this.lblValidateValue.Text = this.dtReqShipDate.Value.ToString("M/d/yyyy");
}
plusone3055

are you firing this method when you click a button ?

please post the whole routine so I can see whats going on please
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Member_2_4619940

ASKER
That is the whole method from my test project.

But, more info...
Inside the InitializeComponent() method:
this.dtReqShipDate.Validating += new System.ComponentModel.CancelEventHandler(this.dtReqShipDate_Validating);

I've updated a zip file of the test form I used to recreate the issue.
WinTest.zip
it_saige

I cannot reproduce the problem using the files from your zip.  Is there something else that needs to occur.

What I tried:

Modified the date using the keyboard and tabbed out of the control.  Label updates with new value.
Modified the date using the keyboard and moused out of the control.  Label updates with new value.
Modified the date using the date dropdown and tabbed out of the control.  Label updates with new value.
Modified the date using the date dropdown and moused out of the control.  Label updates with new value.

-saige-
Member_2_4619940

ASKER
We've since discovered that this happens when only 1 of the date items is updated.

So...
Use mouse to click into the date field.
Use keyboard to update the default date from 3/4/2015 to 3/5/2015. Only update the 4 to a 5, do not update the month or year.
Use mouse to set focus to the other text box.

Could there be some problem with my installation of VS2010?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
it_saige

Let me try your steps to see if I can reproduce.  Also, how many date time inputs are you using?

-saige-
it_saige

Just tried your steps.  Did not reproduce the same error.  Is it possible that you have multiple Date Time Inputs using the same validate method?

To test use the following for your validate method:
private void dtReqShipDate_Validating(object sender, CancelEventArgs e)
{
	if (sender as Control != dtReqShipDate)
		MessageBox.Show("Called from {0}", (sender as Control).Name);
	lblValidateValue.Text = dtReqShipDate.Value.ToString("M/d/yyyy");
}

Open in new window

-saige-
Member_2_4619940

ASKER
IT_SAGE- I added your suggested snippet into my test project. I still get the same behavior. No prompt about it getting called from another object.

I now suspect that there is something wacky with my installation of VS2010. Or, maybe not.

The guy sitting next to me just tried my test form and is able to recreate the behavior.

It happens if we click into the field and change the month or date with the keyboard (using the number keys, not the arrow keys.) Using the arrow keys results in correct behavior. Changing the year works correctly as well.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
it_saige

Are you using the number pad keys or the number keys from the top row?

-saige-
it_saige

Ok, I *finally* was able to reproduce your issue using the number row keys and the number pad keys.  Let me do some debugging.

-saige-
Member_2_4619940

ASKER
Yes... using the number row keys.
This laptop does not have a number pad.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
it_saige

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Member_2_4619940

ASKER
Great. I'll check it out later today.
Thanks.
Member_2_4619940

ASKER
I really hate having to use hacks like this.
But, it works great. Thanks.
it_saige

Agreed.

-saige-
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck