问题
I am using a datetime picker in c# windows forms application.
How to set the the min time and max time ? I have a string "07:52:22" and I want to set this as the max or min time. How can I do this ?
DatetimePicker.MinDate.TimeOfDay = "07:52:22";
This is wrong but this is what I want.
回答1:
DateTimePicker datePicker = new DateTimePicker;
dateTimePicker.MinDate = DateTime.Parse("7:52:22");
回答2:
Right, not supported. You'll have to add the validation yourself with the ValueChanged event. You could just limit it like this:
private void dateTimePicker1_ValueChanged(object sender, EventArgs e) {
var max = new TimeSpan(7, 52, 22);
if (dateTimePicker1.Value.TimeOfDay >= max) {
dateTimePicker1.Value = dateTimePicker1.Value.Date + max;
}
}
来源:https://stackoverflow.com/questions/18934795/how-to-set-max-time-and-min-time-in-c-sharp-datetime-picker