C# and Month Calendar, selecting multiple dates

若如初见. 提交于 2019-12-07 11:41:36

问题


I am making a program that will help people "book" orders for a department in C#. They need to be able to choose multiple dates in different months.

I would prefer to have it so they can click a date, and then shift click another one to select all dates between those two, and control clicking as well, to do single selection/deselection. They have to be able to move between months while still retaining all the dates they clicked for the previous month, this way they can overview the dates they've selected to make it easier.

What is the best way to do this? Should I use Visual Studio's default month calendar or is there a more flexible one that exists?


回答1:


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.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/1738824/c-sharp-and-month-calendar-selecting-multiple-dates

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