简体   繁体   English

如何在DateTimePicker winforms c#中启用/禁用特定日期

[英]how to enable/disable specific dates in DateTimePicker winforms c#

I am programming a C# Windows application for a clinic and i stored days of works for every doctor for example我正在为诊所编程一个C# Windows应用程序,例如,我为每个医生存储了几天的工作

Dr.John works every Monday and Tuesday how i can enable dates in DateTimePicker for dates that only match the specific days and disable other days . Dr.John 每周一和周二工作,我如何在DateTimePicker为仅匹配特定日期的日期启用日期并禁用其他日期。

I don't know what are the methods and functions can help in that我不知道有哪些方法和功能可以帮助

Instead of the DateTimePicker you can而不是DateTimePicker你可以

  • create a form on the fly即时创建表单
  • add a MonthCalendar to it添加一个MonthCalendar到它
  • add either valid or invalid dates to the BoldDates collection将有效或无效的日期添加到BoldDates集合
  • code the DateChanged event编写DateChanged事件
  • test to see if a valid date was selected测试以查看是否选择了有效日期
  • add it to the list of dates picked将其添加到所选日期列表中

Details depend on what you want: A single date or a range, etc.详细信息取决于您想要的内容:单个日期或范围等。

Make sure to trim the time portion, mabe like this for adding dates:确保修剪时间部分,像这样添加日期:

List<DateTime> bold = new List<DateTime>();
for (int i = 0; i < 3; i++)
    bold.Add(DateTime.Now.AddDays(i*3).Date);

monthCalendar1.BoldedDates = bold.ToArray();

To select only valid dates maybe code like this:要仅选择有效日期,代码如下:

List<DateTime> selected = new List<DateTime>();

private void monthCalendar1_DateSelected(object sender, DateRangeEventArgs e)
{
    for   (DateTime dt = monthCalendar1.SelectionStart.Date; 
                    dt.Date <= monthCalendar1.SelectionEnd.Date; 
                    dt = dt.AddDays(1))
    {
        if (!monthCalendar1.BoldedDates.Contains(dt)
        && !selected.Contains(dt)) selected.Add(dt.Date);
    }
}

Unfortunately the options to set any stylings are limited to bolding dates.不幸的是,设置任何样式的选项仅限于加粗日期。 No colors or other visual clues seem to be possible.似乎不可能有颜色或其他视觉线索。

So for anything really nice you will have to build a date picker yourself..因此,对于任何非常好的事情,您都必须自己构建一个日期选择器。

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

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