在代码中设置时间选择器值 [英] Set timepicker value in code
问题描述
是否可以在代码中设置时间选择器的值。
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.
但是如果我不选择时间值,则应将00:00设置为默认值,而我想使用代码来做到这一点。
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.
更新
这就是我思考:
DateTime? temp = (DateTime)startDate.Value;
DateTime date1;
if (temp == null)
{
//Set the time to 00:00
}
else
{
date1 = (DateTime)temp;
}
update2
这是所有代码并得到一个例外,这就是为什么我要在代码中设置值的原因,因为接受可为空的datetime似乎不起作用。
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);
推荐答案
如果您已经覆盖了 DateTimePicker
控件只是一个 TimePicker
控件,您只需要设置 text
属性
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";
如果您仍然有 DateTimePicker
并想要只需将以下属性设置为 TimePicker
:
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;
我的建议是设置默认的 Text
提前设置值,以便如果用户不输入时间,它将包含您已经指定的默认时间。
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.
编辑:为<$ c设置默认值$ c> 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
}
类似的东西应该可以为您带来想要的结果。
Somthing like that should get you the results you want.
这篇关于在代码中设置时间选择器值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!