如何在 DateTimepicker 中禁用/限制年份选择 [英] How to Disable/Restrict selection of year in DateTimepicker
问题描述
我使用 datetimepicker 只选择日期和月份,年份无关紧要.
I am using datetimepicker to select only day and month, Year is irrelevant.
如何禁用/限制用户选择年份.
How do I disable/restrict the year selection by user.
或者更确切地说,当用户尝试选择日期时不应显示年份
or rather year shouldn't be shown when user tries to select the date
推荐答案
如果您限制用户可以在 DateTimePicker
上选择的两个日期,并为选择器本身设置自定义格式,您可以有效地拥有选择日期时间的多合一向上/向下控件.如果年份不重要,您可以将 MinDate
和 MaxDate
值设置为闰年以获得完整范围.这当然意味着您需要在后端代码中提取月/日并按照您的意愿进行处理.
If you restrict the two dates that the user can choose between on the DateTimePicker
as well as set a custom format for the picker itself, you can effectively have an all in one Up/Down control that selects date time. If year is not important, you can set the MinDate
and MaxDate
values to a leap year to get a full range. This of course means that you'll need to extract the Month/Day out in the back end code and process it how you wish.
以下是设计器提供的所有选项.
Here are the options, all available from the designer.
dateTimePicker1.MinDate = new DateTime(2012, 01,01);
dateTimePicker1.MaxDate = new DateTime(2012,12,31);
dateTimePicker1.ShowUpDown = true;
dateTimePicker1.Format = DateTimePickerFormat.Custom;
dateTimePicker1.CustomFormat = "MMMM dd";
这篇关于如何在 DateTimepicker 中禁用/限制年份选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!