C# and Month Calendar, selecting multiple dates
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 the开发者_运维问答n 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?
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.
精彩评论