禁用某些日期的DateTimePicker的WinForm C#日期、DateTimePicker、WinForm

2023-09-03 05:29:37 作者:青笺画卿颜

我需要禁用某些日期,的DateTimePicker,这可能吗? 例如,我有一个日期范围:{20/12/2014,21/12/2014,22/12/2014,23/12/2014,24/12/2014} 我需要禁用midle一个或在其他情况下,最后一个,任何人可以帮助我吗?

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?

在dateTimePicker1.MainDate和dateTimePicker1.MaxDate是远远不够的。

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

感谢你。

推荐答案

从.net 4开始,可以在WPF。然后,您可以承载WPF 日期选择器里面的WinForms应用程序。 (从MSDN)的例子来禁用周六和周日:

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);

在MSDN上的更多细节: HTTP:/ /msdn.microsoft.com/en-us/library/k7z0zy8k%28v=vs.110%29.aspx

我还没有看到任何类似,这将是专门为的WinForms,虽然。作为一种变通方法,您可以口这WPF组件到的WinForms应用程序。

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.

下面的教程上的如何使用WPF中的WinForms 控制:

http://tech.pro/tutorial/799/ WPF的教程使用-WPF合的WinForms