问题
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);
回答1:
If you have already overridden the DateTimePicker
control to be only a TimePicker
you just need to set the text
property of the control.
myTimePicker.Text = "00:00:00";
If you still have a DateTimePicker
and want to make it only a TimePicker
just set the following properties:
myTimePicker.Format = DateTimePickerFormat.Time;
myTimePicker.ShowUpDown = true;
My suggestion is to set the default Text
value ahead of time so that if the user does not input a time, it will contain the default time you specify already.
EDIT: Setting default value for your DateTime
object.
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
}
Somthing like that should get you the results you want.
来源:https://stackoverflow.com/questions/16898119/set-timepicker-value-in-code