简体   繁体   中英

DateTimePicker changing value doesn't fire value changed event - Winforms

I'm facing weird situation with DateTimePicker control. I've a DateTimePicker control with short date format.

I'm setting the value of this control in the textbox in the following way:

private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
    {
      textBox1.Text = dateTimePicker1.Value.ToShortDateString();
    }

Now, when I try to change the month, which is first section in the control, if I enter two digits it fires ValueChanged event otherwise not.

Say, for example I'm trying to enter 6th month and I press 6 on keyboard, this time ValueChanged event doesn't fire. But when I press 0 and 6 then it fires that event. If I enter 11 or 12 it fires ValueChanged event. Same applies to date section as well.

In short it doesn't fire ValueChanged event when I enter single digit in Date or Month section but it fires when I enter two digits. Can anybody tell me why it is behaving this way? Is it expected behaviour of DTP?

How do I make it fire that event even when I enter single digit?

For consistency and simplicity the field takes 2 digits and so should wait for both to be entered to fire the event. Should it fire when you enter 1 to represent January even though it does not know you might enter another digit for Oct (0), Nov (1) or Dec (2)

I would expect if you entered 6 and tab away the event would fire.

Although you might be able to do something on the TextChanged event which should get fired if you type a single digit although I am not sure it will work when using the mouse to select the date (best to check that as I recall a while back I had issues with it not firing)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM