Question

Heyo,

I have a standard WinForms MonthCalendar in my application with a handler hooked up to the DateChanged event. Whenever I change the date by day, clicking on a certain date in the little calendar, the event fires once. However, whenever I change the date by month, clicking on the < and > in the control, the event fires twice. I want the event to fire once in all situations.

Any ideas?

EDIT: I debugged and found out that the SelectedItem or Range is the same on the first and second handler call. So I need a way to differentiate between the first and second call while still allowing for proper handling when the event only fires once.

The handler code was requested, here it is, but it has nothing to do with the event firing multiple times:

List<TimestampInfo> displayTimestamps = databaseManger.QueryForTimestamps(DayPicker.SelectionRange);
if (displayTimestamps == null) return;
TimestampsListBox.Items.Clear();
TimestampsListBox.Items.AddRange(displayTimestamps.ToArray());
Was it helpful?

Solution

Somewhat of a hack, but compare the SelectionRange string value with the last DataChanged event. Just run your code if it's different:

private string _LastRange = string.Empty;

private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e) {
  if (monthCalendar1.SelectionRange.ToString() != _LastRange) {
    _LastRange = monthCalendar1.SelectionRange.ToString();

    List<TimestampInfo> displayTimestamps = databaseManger.QueryForTimestamps(DayPicker.SelectionRange);
    if (displayTimestamps == null) return;
    TimestampsListBox.Items.Clear();
    TimestampsListBox.Items.AddRange(displayTimestamps.ToArray());
  }
}

OTHER TIPS

I couldn't reproduce this until I hooked up the event handler twice.

monthCalendar1.DateChanged += new System.Windows.Forms.DateRangeEventHandler(this.monthCalendar1_DateChanged);
monthCalendar1.DateChanged += new System.Windows.Forms.DateRangeEventHandler(this.monthCalendar1_DateChanged);

Is you code munging around with the event handlers?

Try this:

private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e) {
      Calendar1.SelectedDate = Calendar1.VisibleDate;
      //  any additional code optional
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top