Link to home
Start Free TrialLog in
Avatar of freename
freenameFlag for United States of America

asked on

Why does DateTimePicker event fire twice in a row?

Using VS2003. Why does this event fire twice, even though the DateTimePicker value is change. The first time through, dtp.value is less than dtpBeginning.value. However, the value is changed. I understand by changing the value, it causes the event to fire again. But this time dtp.value is equal to dtpBeginning.value. Yet the if statement returns false and message box is displayed a second time. I have tried to unsubscribe the event just before changing the value, then subscribing back to it after the value change.
private void dtpEnd_ValueChanged(object sender, System.EventArgs e)
        {
            // Get the date picker
            DateTimePicker dtp = (DateTimePicker) sender;
 
            // Make sure end date is not before beginning date
            if (dtp.Value < dtpBeginning.Value) 
            {
                // Send message box to inform user
                string msg = "End date cannot be set before beginning date";
                MessageBox.Show(msg, "Date Selection Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                dtp.Value = dtpBeginning.Value;
            }
        }

Open in new window

Avatar of freename
freename
Flag of United States of America image

ASKER

Excuse me. Statement "Yet the if statement returns false" should read "Yet the if statement returns true".
Can you post the rest of the code? Including any code that may subscribe to the ValueChanged event?
It is firing twice because of this:

dtp.Value = dtpBeginning.Value;

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of ZachSmith
ZachSmith
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks. I had to unsubscribe to Value Changed event. I also moved the message box to the close_up event. Now it works like originally envisioned.