如何在DateTimePicker Winforms中启用/禁用特定日期C# [英] how to enable/disable specific dates in DateTimePicker winforms c#

查看:153
本文介绍了如何在DateTimePicker Winforms中启用/禁用特定日期C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为诊所编写 C# Windows 应用程序,例如,我为每个医生存储了工作日

I am programming a C# Windows application for a clinic and i stored days of works for every doctor for example

约翰博士每个星期一和星期二工作,我如何在 DateTimePicker 中启用仅与特定日期匹配的日期几天并禁用其他日子。

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 .

我不知道有什么方法和函数可以帮助您解决问题

I don't know what are the methods and functions can help in that

推荐答案

您可以


    代替 DateTimePicker li>动态创建表单
  • 向其中添加 MonthCalendar

  • 添加 BoldDates 集合的有效日期或无效日期

  • 编码 DateChanged 事件

  • 测试以查看是否选择了有效日期

  • 将其添加到选择的日期列表中

  • create a form on the fly
  • add a MonthCalendar to it
  • add either valid or invalid dates to the BoldDates collection
  • code the DateChanged event
  • 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..

这篇关于如何在DateTimePicker Winforms中启用/禁用特定日期C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆