How to set max time and min time in c# datetime picker

风流意气都作罢 提交于 2020-02-05 07:19:28

问题


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

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