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