views:

838

answers:

2

Winforms 2.0. Stick a DateTimePicker on a form. It defaults to TODAY. Click on the dropdown arrow to show the Calendar, and click on TODAY. The ValueChanged event DOES FIRE, even though it is already set to today.

What i would like to do is replicate this same functionality - I would like to reset it (in code) to today, and next time i click on today via the Calendar i want that ValueChanged event to fire (just like it did the first time).

If the above is not possible, i want some event that always fires whenever i pick a date on the Calendar (regardless if there is a change or no change).

Sounds really simple, surely someone has the answer?

A: 

Im not a win form programmer, but see if there is a Day_Render event. That should fire for each day...at least there is one for web based development, but I would think it is the same. So look for Render() event.

JonH
+2  A: 

What you're seeing there is a side-effect of the fact that DateTimePicker stores the time component of its value as well as the date.

So when your form first initializes, the DateTimePicker is being initialized with DateTime.Now. When you then select "Today" from its drop-down calendar, you're changing the value to DateTime.Today.

If you don't care about the time component and/or you're always reading the value as dateTimePicker1.Value.Date, then you could initialize the control in code with DateTime.Now and the ValueChanged event will fire when the user selects Today from the calendar. Otherwise you might consider catching the CloseUp event, which is fired whenever the calendar control is closed (regardless of whether the value changed).

Matt Hamilton
Thank you. Your explanation makes sense, i was re-setting it to DateTime.Today during experimentation, it should have been DateTime.Now as you pointed out. Good to know about the CloseUp event too, but i didn't use it in my case as i wanted to specifically react to clicking on a Calendar date, and not anywhere on the form.
joedotnot