Limiting the dates within a C# win form DateTimePi

2020-02-07 04:02发布

Is it possible to limit which dates a user can select from a dateTimePicker on a C# winforms application?

The basic principle for me is this: I have a comboBox with 5 items in it, based on which item the user selects I would like to limit which dates the user can then select from, having the unavailable dates grayed out.

Is this possible?

5条回答
Animai°情兽
2楼-- · 2020-02-07 04:13

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.

查看更多
Viruses.
3楼-- · 2020-02-07 04:19

Use the MinDate and MaxDate properties.

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

DateTimePicker
(render on a french Windows 7)

查看更多
孤傲高冷的网名
4楼-- · 2020-02-07 04:21

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.
查看更多
倾城 Initia
5楼-- · 2020-02-07 04:22

Yes, at least MSDN says so. Refer here.

查看更多
Deceive 欺骗
6楼-- · 2020-02-07 04:29

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.

查看更多
登录 后发表回答