Disable some dates in datetimepicker winform c# [duplicate]

匿名 (未验证) 提交于 2019-12-03 01:18:02

问题:

This question already has an answer here:

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.

回答1:

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



回答2:

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?



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