How to Disable/Restrict selection of year in DateTimepicker

时光怂恿深爱的人放手 提交于 2021-01-29 17:42:09

问题


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


回答1:


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";


来源:https://stackoverflow.com/questions/9613318/how-to-disable-restrict-selection-of-year-in-datetimepicker

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