Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable some dates in datetimepicker winform c# [duplicate]

Tags:

c#

.net

winforms

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.

like image 497
Mateus Caires Avatar asked Dec 05 '14 15:12

Mateus Caires


1 Answers

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

like image 90
PiotrWolkowski Avatar answered Nov 16 '22 18:11

PiotrWolkowski