how to enable/disable specific dates in DateTimePi

2019-07-26 19:19发布

问题:

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