开发者

DateTimePicker ValueChanged Event repeats with month arrow

When the month forward or backward arrow is clicked on my DateTimePicker control it repeatedly fires the ValueChanged event. I have to use the debugger to stop the application.

Note: My application works fine as long as I click o开发者_StackOverflown one of the dates.

The MSDN documentation shows examples for creating the control. But I cannot find any example function called dateTimePicker1_ValueChanged(). The skeleton for this function was created for me, when I double clicked on the control in the VS.NET2008 designer.


It is not clear to me what you are trying to achieve but my guess is that you want for example show a messagebox or something similar. If so then no fears because you are facing the very same issue I was wondering some time ago. Instead of using the ValueChanged -event, use the CloseUp -event. CloseUp -event is triggered only when the user finally selects a value. Hope this was what you were looking for. If would want to update for example some calculations shown to user in the UI you would use ValueChanged -event.


Dave81 set me on to the right track. If you do what he says, the problem is, that when the user changes the value manually, the CloseUp event obviously isn't triggered. The way I've fixed this problem with the constant re-firing of the month change event, is to change my ValueChanged event to only refresh if the calendar isn't dropped down. I.e.:

private bool _calendarDroppedDown = false;
//called when calendar drops down
private void dateStartDateTimePicker_DropDown(object sender, EventArgs e)
{
  _calendarDroppedDown = true;
}
//called when calendar closes
private void dateStartDateTimePicker_CloseUp(object sender, EventArgs e)
{
  _calendarDroppedDown = false;
  RefreshToolbox(null, null); //NOW we want to refresh display
}

//This method is called when ValueChanged is fired
public void RefreshToolbox(object sender, EventArgs e)
{ 
  if(_calendarDroppedDown) //only refresh the display once user has chosen a date from the calendar, not while they're paging through the days.
    return;
  ...
}


When you click on the down arrow of a datetimepicker, it displays a default date. When you click on any date in the datetime picker, it fires the datetimepicker1_valuechanged event. Whatever you have put the skeleton for the event, works fine then. Unnikrishnan C

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜