Limiting the dates within a C# win form DateTimePicker

孤者浪人 提交于 2019-11-28 13:58:46

Use the MinDate and MaxDate properties.

dateTimePicker.MinDate = DateTime.Now;
dateTimePicker.MaxDate = DateTime.Now.AddDays(15);


(render on a french Windows 7)

You can set a minimum and a maximum date for the C# WinForms DTP, so if thats what you wish to do, then you can use the MinValue and MaxValue variables. You can't pick and choose blocks of dates that are not allowed. This is something you would have to add yourself. There are 2 possible methods of doing this:

  • Handing the ValueChanged event, then validate the date chosen.
  • Inherit the DTP class and add some extra functionality in there.

The DateTimePicker control has MaxDate and MinDate properties. Set those, and you can control the range of dates that can be selected. Currently on my Windows XP with Windows Classic theme the unselectable dates do not appear grayed, but this may vary depending on operating system, theme, etc. If you absolutely must gray them, then you will have to subclass the DateTimePicker control and do the (or part of the) painting yourself.

Yes, at least MSDN says so. Refer here.

You can set up the date restrictions in the following manner

dateTimePicker1.MinDate = DateTime.Today.AddDays(-2);
dateTimePicker1.MaxDate = DateTime.Today.AddDays(2);

In these case only the 5 available dates would be selectable for the user and the rest is not available. You could set up these values in the selectedindex changed event for the combobox and restrict it on the basis of your requirement/logic.

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