简体   繁体   English

自定义Datetimepicker,将允许启用/禁用某些日期

[英]Custom Datetimepicker that will allow to enable/disable certain dates

I need to develop a custom DateTimePicker that will allow me to enable/disable certain dates from the calendar for allowing/disallowing user to select them. 我需要开发一个自定义DateTimePicker ,它将允许我从日历中启用/禁用某些日期,以允许/禁止用户选择它们。

Can anyone help? 有人可以帮忙吗?

You can set blackdays in the DatePicker, maybe that helps. 您可以在DatePicker中设置黑日,这可能有所帮助。

See documentation DatePicker.BlackoutDates 请参阅文档DatePicker.BlackoutDates

You can handle the ValueChanged event to handle manually your date validation. 您可以处理ValueChanged事件以手动处理日期验证。 See MSDN doc . 请参阅MSDN doc

Something like this will to the trick: 这样的东西将达到目的:

private void DateTimePicker_ValueChanged(Object sender, EventArgs e) 
{
    var selectedDate = this.DateTimePicker.Value;
    if (this.DateIsForbidden(selectedDate))
    {
        var forcedDate = this.GetAllowedDate(selectedDate);
        this.DateTimePicker.Value = forcedDate;
        MessageBox.Show(string.Format("Date {0} is not allowed. {1} selected instead", selectedDate, forcedDate));
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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