7

I need to disable some dates into DateTimePicker, is it possible? For example, I have a dates range: { 20/12/2014, 21/12/2014, 22/12/2014, 23/12/2014, 24/12/2014 } I need to disable the midle one or in others situations the last one, can anybody help me?

The dateTimePicker1.MainDate and dateTimePicker1.MaxDate is not enough.

Thank you.

2
  • While it is a bit more work, Piotr Wolkowski's answer is a decent approach using pure .Net. Otherwise @Anthony found a useful link for you as well.
    – AWinkle
    Dec 5, 2014 at 16:03
  • This question shouldn't be marked as duplicate. The question they are referring has low quality accepted answer.
    – Szybki
    Dec 3, 2017 at 0:05

2 Answers 2

3

Starting from .Net 4 it is possible in WPF. You can then host the WPF datepicker inside a Winforms app. An example (from MSDN) to disable Saturdays and Sundays:

DatePicker datePickerWithBlackoutDates = new DatePicker();

datePickerWithBlackoutDates.DisplayDateStart = new DateTime(2009, 8, 1);
datePickerWithBlackoutDates.DisplayDateEnd = new DateTime(2009, 8, 31);
datePickerWithBlackoutDates.SelectedDate = new DateTime(2009, 8, 10);

datePickerWithBlackoutDates.BlackoutDates.Add(
    new CalendarDateRange(new DateTime(2009, 8, 1), new DateTime(2009, 8, 2)));
datePickerWithBlackoutDates.BlackoutDates.Add(
    new CalendarDateRange(new DateTime(2009, 8, 8), new DateTime(2009, 8, 9)));
datePickerWithBlackoutDates.BlackoutDates.Add(
    new CalendarDateRange(new DateTime(2009, 8, 15), new DateTime(2009, 8, 16)));
datePickerWithBlackoutDates.BlackoutDates.Add(
    new CalendarDateRange(new DateTime(2009, 8, 22), new DateTime(2009, 8, 23)));
datePickerWithBlackoutDates.BlackoutDates.Add(
    new CalendarDateRange(new DateTime(2009, 8, 29), new DateTime(2009, 8, 30)));

datePickerWithBlackoutDates.DateValidationError +=
new EventHandler<DatePickerDateValidationErrorEventArgs>(DatePicker_DateValidationError);

More details on MSDN: http://msdn.microsoft.com/en-us/library/k7z0zy8k%28v=vs.110%29.aspx

I haven't seen anything similar that would be specifically for Winforms though. As a workaround you can port this WPF component into Winforms app.

Here's MSDN resource on interopeartion of WPF and Winforms:

https://msdn.microsoft.com/en-us/library/ms751797(v=vs.100).aspx

4
  • I was just going to downvote this, since the OP intends this to be used in WinForms, but the power of Google compels me to upvote you. Use WPF controls in WinForms
    – AWinkle
    Dec 5, 2014 at 16:01
  • There is a link added in the post to a tutorial how to do that. Just in case I'll make it bit more visible. Dec 5, 2014 at 16:08
  • That link appears to be broken ...
    – Mike Honey
    Jan 6, 2016 at 11:16
  • @MikeHoney I replaced it with an MSDN resource. Jan 6, 2016 at 13:02
0

You can specify a max date, but you cannot selectively disable arbitrary dates in the valid range.

Please see How do I disable some dates on a DateTimePicker control?

Not the answer you're looking for? Browse other questions tagged or ask your own question.