C# and Month Calendar, selecting multiple dates
Asked Answered
M

3

5

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?

Mannes answered 15/11, 2009 at 21:2 Comment(2)
It's going to run on Windows, no webforms.Mannes
Napoli, 'Windows' could still be WinForms or WPF. Either would be a much more useful Tag than 'studio'.Chromite
D
8

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.

Disini answered 15/11, 2009 at 23:19 Comment(0)
C
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.

Chromite answered 15/11, 2009 at 21:14 Comment(0)
W
0

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.

Whitmer answered 15/11, 2009 at 21:13 Comment(2)
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)Whitmer
Interesting... there's no "SelectionMode" property for my monthCalendar objectMannes

© 2022 - 2024 — McMap. All rights reserved.