如何限制用户在datetimepicker控件中仅选择当前月份日期?

时间:2015-05-12 06:10:53

标签: c# winforms date datetime datetimepicker

我希望用户只选择当前月份的日期我不知道应该在最小和最大日期属性中放入什么。

是否有任何类似于'的财产?本月'?

2 个答案:

答案 0 :(得分:5)

由于MinDateMaxDate属性都需要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);