1

Is it possible to set the timepicker value in code.

What I am thinking here is that if I have a timepicker and choose a time value, thats the time is should use.

But if I dont choose a time value, it should set 00:00 as default and I want to do this with code.

Hade some crazy idea that looked like this:

DateTime date;
date.Hour(00:00:00)

Didnt work that good so is there any good way for making this.

UPDATE

Here is what i am thinking:

DateTime? temp = (DateTime)startDate.Value;
            DateTime date1;
            if (temp == null)
            {
               //Set the time to 00:00
            }
            else
            {
                date1 = (DateTime)temp;
            }

update2 Here is all code and getting an exception and thats why i want to set the value in code becasue accepting nullable datetime didnt seem to work.

  DateTime date = (DateTime)datePicker.Value;
  DateTime break1S = (DateTime)startBreak1.Value;
   _nestedDateStartBreak1 = new DateTime(date.Year, date.Month, date.Day, break1S.Hour, break1S.Minute, 0);
4

1 回答 1

0

如果您已经将DateTimePicker控件重写为仅 a TimePicker,则只需设置text控件的属性。

    myTimePicker.Text = "00:00:00";

如果您仍然有 aDateTimePicker并且只想将其设置为 a TimePicker,只需设置以下属性:

    myTimePicker.Format = DateTimePickerFormat.Time;
    myTimePicker.ShowUpDown = true;

我的建议是提前设置默认Text值,这样如果用户没有输入时间,它将包含您已经指定的默认时间。

DateTime编辑:为您的对象设置默认值。

    DateTime defaultDate = new DateTime(); 
    defaultDate = Convert.ToDateTime("2013-12-31 00:00:00"); //Or whatever you want your default value to be...
    if (startDate.Value == null) 
    { 
        startDate.Value = defaultDate; 
    } 
    else 
    { 
        //do something else with the value 
    }

类似的东西应该会给你你想要的结果。

于 2013-06-03T13:53:48.100 回答