DatePicker allows selecting a date.
UPDATE: I would like to force DatePicker to allow only entry as dd/mm/yyyy. So, if user tries to enter "December 21, 2015" it would not allow it.
I am in .NET Framework 4.5
To catch cases where the text typed into the DatePicker is not a valid date format you should handle the DateValidationError
event . You'll get passed a System.FormatException
if the format isn't acceptable.
To limit the drop-down Calendar to the current year you can set the DisplayDateStart
and DisplayDateEnd
properties - but that doesn't stop you typing other dates in the text box.
MyDatePicker.DisplayDateStart = new DateTime(DateTime.Now.Year, 1, 1);
MyDatePicker.DisplayDateEnd = new DateTime(DateTime.Now.Year, 12, 31);
To limit the DatePicker to the current year set the BlackoutDates
property to exclude all dates other than the current year. For example:
// Exclude all dates before the start of this year
MyDatePicker.BlackoutDates.Add(
new CalendarDateRange(DateTime.MinValue, new DateTime(DateTime.Now.Year - 1, 12, 31)));
// Exclude all dates after the end of this year
MyDatePicker.BlackoutDates.Add(
new CalendarDateRange(new DateTime(DateTime.Now.Year + 1, 1, 1), DateTime.MaxValue));
A validly formatted date that falls into one of the blackout date ranges will cause an ArgumentOutOfRangeException to be passed to the DateValidationError event handler
Use the following code:
DateTimePicker.Format = DateTimePickerFormat.Custom
DateTimePicker.CustomFormat = "dd-MM-yyyy"
Try this to validate the date range starting from today to 30 more days.
System.DateTime today = System.DateTime.Today;
this.DateTimePicker.MinDate = new System.DateTime(today.Year, today.Month, today.Day);
this.DateTimePicker.MaxDate = today.AddDays(30);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.