如何从dateTime选择器和组合框中获取值并将其设置为dateTime变量?在c#中

时间:2015-04-09 18:49:24

标签: c# winforms combobox datetimepicker

我在c#中有一个窗体,用户必须从日期时间选择器中选择一个日期,并从组合框中选择一个时间。但是我如何将它们存储在相同的DateTime变量中?

1 个答案:

答案 0 :(得分:0)

假设你有两个单独的字段,DateTime Picker返回一个DateTime对象(pickerValue),而Combobox返回一个字符串值(dropdownValue):

DateTime finalValue;            
try
{
    TimeSpan ddValue = TimeSpan.Parse(dropdownValue);
    finalValue = pickerValue.Date.AddTicks(ddValue.Ticks);
}
catch (Exception)
{
    //handle exception
    // most likely dropdownValue is not properly formatted timeString.
    finalValue = pickerValue.Date; //or however you would like to handle
}