【问题标题】:Custom Datetimepicker that will allow to enable/disable certain dates允许启用/禁用某些日期的自定义日期时间选择器
【发布时间】:2014-12-02 08:32:49
【问题描述】:

我需要开发一个自定义 DateTimePicker,它允许我启用/禁用日历中的某些日期,以允许/禁止用户选择它们。

谁能帮忙?

【问题讨论】:

标签: c# .net winforms custom-controls datetimepicker


【解决方案1】:

您可以在 DatePicker 中设置黑日,也许这会有所帮助。

参见文档DatePicker.BlackoutDates

【讨论】:

【解决方案2】:

您可以处理ValueChanged 事件来手动处理您的日期验证。见MSDN doc

这样的事情会成功的:

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

【讨论】:

  • 是的,但我们希望将日期显示为灰色,以便用户不会选择它们并且不会发生值更改事件,用户应该能够识别它已禁用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-22
  • 2020-08-25
相关资源
最近更新 更多