我希望用户只选择当前月份的日期我不知道应该在最小和最大日期属性中放入什么。
是否有任何类似于'的财产?本月'?
答案 0 :(得分:5)
由于MinDate
和MaxDate
属性都需要DateTimePicker1.MinDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
DateTimePicker1.MaxDate = (new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1))
.AddMonths(1).AddDays(-1);
,因此您可以设置它们;
{{1}}
答案 1 :(得分:1)
是的,您可以使用属性MinDate和MaxDate,并设置可在控制中选择的最小和最大日期。
var currentYear = DateTime.Now.Year;
var currentMonth = DateTime.Now.Month;
int firstDayInCurrentMonth = 1;
int lastDayInCurrentMonth = DateTime.DaysInMonth(currentYear, currentMonth);
dateTimePicker.MinDate = new DateTime(currentYear, currentMonth, firstDayInCurrentMonth);
dateTimePicker.MaxDate = new DateTime(currentYear, currentMonth, lastDayInCurrentMonth);