views:

602

answers:

2

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?

A: 

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.

Cyborgx37
I am using the WPF Toolkit for .NET 3.5, but I can set my Calendar's SelectionMode property to "MultipleRange" which allows for multiple ranges to be selected. The DisplayDateStart and DisplayDateEnd properties allow you to set the range of dates that can be selected (for example, setting your DisplayDateStart to 1/1/2010 would prevent the user from selecting a date before 1/1/2010)
Cyborgx37
Interesting... there's no "SelectionMode" property for my monthCalendar object
Napoli
+4  A: 

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.

Hans Passant
Clever trick. With added cheap motel features.
Henk Holterman