如何将 MaskedTextBox 值转换为 TimeSpan?
How to convert a MaskedTextBox value to TimeSpan?
到目前为止我试过这个:
try
{
TimeSpan durtime = TimeSpan.Parse(timeDur.ToString());
}
catch (FormatException)
{
MessageBox.Show("Bad Format", timeDur.ToString());
}
timeDur
是掩码类型为 __:__
的 MaskedTextBox(验证类型 DateTime)。
当我尝试将 timedur
MaskedTextBox 转换为字符串格式时,它给了我一个 System.FormatException 类型的异常:
"The string was not recognized as a valid TimeSpan."
您是否考虑过使用格式为 "Time" 的 DateTimePicker?
这样您就不必担心特定于文化的格式等问题。
老实说,我不知道为什么它现在开始工作了,但是
etimeWHP1.Value = btimeWHP1.Value.Add(TimeSpan.Parse(timeDur.Text));
这解决了我的问题。
到目前为止我试过这个:
try
{
TimeSpan durtime = TimeSpan.Parse(timeDur.ToString());
}
catch (FormatException)
{
MessageBox.Show("Bad Format", timeDur.ToString());
}
timeDur
是掩码类型为 __:__
的 MaskedTextBox(验证类型 DateTime)。
当我尝试将 timedur
MaskedTextBox 转换为字符串格式时,它给了我一个 System.FormatException 类型的异常:
"The string was not recognized as a valid TimeSpan."
您是否考虑过使用格式为 "Time" 的 DateTimePicker?
这样您就不必担心特定于文化的格式等问题。
老实说,我不知道为什么它现在开始工作了,但是
etimeWHP1.Value = btimeWHP1.Value.Add(TimeSpan.Parse(timeDur.Text));
这解决了我的问题。