简体   繁体   English

C#和月历,选择多个日期

[英]C# and Month Calendar, selecting multiple dates

I am making a program that will help people "book" orders for a department in C#. 我正在制作一个程序,帮助人们“预订”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? 我应该使用Visual Studio的默认月份日历还是存在更灵活的日历?

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: 实现MonthCalendar的MouseDown事件:

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. WinForms MonthCalendar支持从开始到结束选择范围,但不支持使用Ctrl选择(de)各个日期。 So it seems it does not meet your requirements. 所以它似乎不符合您的要求。

Just a quick note: If you resize the MonthCalendar it will show more months. 只需快速说明:如果您调整MonthCalendar的大小,它将显示更多月份。 Together with nobugz' answer that might give you a working solution. 与nobugz的回答一起,可能会给你一个有效的解决方案。

Assuming that you are using WPF... 假设您正在使用WPF ...

I would recommend that you create a simple ListBox and bind the ItemsSource property to the Calendar's SelectedDates property. 我建议您创建一个简单的ListBox并将ItemsSource属性绑定到Calendar的SelectedDates属性。 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. 此外,您可以创建DateSpan类和ValueConverter,以将系列中的日期分组到DateSpan类中。 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). 然后,您可以将转换器应用于SelectedDates属性,以便当用户使用Shift-Select时,它们将看到日期跨度而不是一堆日期(假设这是一件坏事)。 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. 我能想到的最佳答案是一份清单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM