简体   繁体   中英

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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