C# and Month Calendar, selecting multiple dates

自古美人都是妖i 提交于 2019-12-05 16:47:51

You can make it work by detecting clicks on dates and then add or remove the clicked date from the bolded dates. Implement the MonthCalendar's MouseDown event:

private void monthCalendar1_MouseDown(object sender, MouseEventArgs e) {
  MonthCalendar.HitTestInfo info = monthCalendar1.HitTest(e.Location);
  if (info.HitArea == MonthCalendar.HitArea.Date) {
    if (monthCalendar1.BoldedDates.Contains(info.Time))
      monthCalendar1.RemoveBoldedDate(info.Time);
    else 
      monthCalendar1.AddBoldedDate(info.Time);
    monthCalendar1.UpdateBoldedDates();
  }
}

Just one problem with this, it flickers like a cheap motel. No fix for that.

The WinForms MonthCalendar supports selection of a Range, from Start to End but not the (de)selection of individual dates with Ctrl. So it seems it does not meet your requirements.

Just a quick note: If you resize the MonthCalendar it will show more months. Together with nobugz' answer that might give you a working solution.

Assuming that you are using WPF...

I would recommend that you create a simple ListBox and bind the ItemsSource property to the Calendar's SelectedDates property. As the user selects and deselects days from the Calendar, they will be added to or removed from the list.

In addition, you could create a DateSpan class and a ValueConverter to group dates in a series into your DateSpan class. You could then apply the converter to the SelectedDates property so that when the user uses Shift-Select, they will see a date span rather than a bunch of dates (assuming that's a bad thing). The logic wouldn't be too complex.

There are plenty of third-party tools out there, but no matter which control you use the core problem will remain: you want the user to be aware of all selected items, but you don't want to show every single month that contains a selected day at the same time. The best answer I can think of would be a list.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!