Wallace_

Hi, I have a DateTimePicker on one of my forms and when the user change the date I have some code in the _ValueChanged event handler.

Part of the code has a Confirm MessageBox that allows the user to confirm whether he wants to change the date or not. If the user selects no then I change the date back to the original...

Code

...
resetDate = true;
dtpExpVector.Value = prevDate;
resetDate = false;
...

I deal with the new _ValueChanged event fired by dtpExpVector.Value = prevDate using the booleans above.

My problem is that for some reason the _ValueChagned event appears to be getting fired an extra time at the end with the date the user selected. Does anybody know why

Thanks.

Wallace



Re: Windows Forms General DateTimePicker _ValueChanged event.

Wallace_

I just want to add something. It appears to be the line dtpExpVector.Value = prevDate; that is causing the extra event to be fired. I expect it to fire 1 event but it actually seems to fire 2.

For example if the DateTimePicker shows 22nd March and I change it to 9th March I get the following events:

Event 1: _ValueChanged(9th March)

If the user then rejects the change the line dtpExpVector.value = prevDate; is called so:

Event 2: _ValueChanged(22nd March).

This is what I expect but a 3rd Event gets called:

Event 3: _ValueChanged(9th March).

It is this 3rd event that I can't explain.

Thanks,

Wallace






Re: Windows Forms General DateTimePicker _ValueChanged event.

nobugz

Changing the property that caused an event to run in the event handler is a reliable way to drive a Windows control nuts. DTP is no exception, it is a troubled control in many ways. I can reproduce your problem. Here's a workaround:

DateTime prevdate;
private void dateTimePicker1_Enter(object sender, EventArgs e) {
prevdate = dateTimePicker1.Value;
}
private void dateTimePicker1_Leave(object sender, EventArgs e) {
if (prevdate == dateTimePicker1.Value) return;
if (DialogResult.Yes == MessageBox.Show("Are you sure", "Date changed", MessageBoxButtons.YesNo)) return;
dateTimePicker1.Value = prevdate;
}






Re: Windows Forms General DateTimePicker _ValueChanged event.

ULeas

Another way is just have some flags to stop recursion:

bool handle = true;

void Control_OnValueChanged( ... ){
if(!handle)
return;
handle = false;

control.Value = "new_value";

handle = true;
}





Re: Windows Forms General DateTimePicker _ValueChanged event.

Wallace_

nobugs this could be the way to do it except that the leave event only gets fired when I actually change focus. Is there any way to fire the event when either the user selects the new date or the calendar disappears.

Thanks





Re: Windows Forms General DateTimePicker _ValueChanged event.

Wallace_

I have tried this but it doesn't work. The 3rd event gets fired after the flag has been set to true so it still happens.

Thanks





Re: Windows Forms General DateTimePicker _ValueChanged event.

Wallace_

the closeup event did the trick.

Thanks for the help...