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

扶醉桌前 提交于 2019-12-11 05:52:49

问题


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

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


回答1:


Instead of the DateTimePicker you can

  • 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..



来源:https://stackoverflow.com/questions/40059200/how-to-enable-disable-specific-dates-in-datetimepicker-winforms-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!