我觉得这个问题的标题不好,但我找不到更好的了。
我想做的是让用户能够设置倒计时计时器。 (看图一):
应用程序接受用户输入并检查用户选择的时间单位,然后将其转换为秒并将值分配给 int 变量。
private int seconds = -1;
private void enable_button_Click(object sender, EventArgs e)
{
int amount = Convert.ToInt32(time_numericUpDown.Value);
string unit = tUnits_comboBox.Text;
// Check what time is chosen then convert it to seconds
if (unit == "Seconds")
seconds = amount;
else if (unit == "Minutes")
seconds = amount * 60;
else if (unit == "Hours")
seconds = amount * 3600;
else if (unit == "Days")
seconds = amount * 86400;
// Clock it!
timer.Enabled = true;
}
然后,计时器应该以人类可读的格式显示时间,我为此使用了这段代码:
private void timer_Tick(object sender, EventArgs e)
{
// Verify if the time didn't pass
if (seconds == 0)
{
// If the time is over, do the specified action
timer.Enabled = false;
operation(); // << This is the function that does the Action!
}
else
{
// Continue counting
seconds -= 1;
TimeSpan timeSpan = TimeSpan.FromSeconds(seconds);
string answer = string.Format("{0:D2}h:{1:D2}m:{2:D2}s",
timeSpan.Hours,
timeSpan.Minutes,
timeSpan.Seconds);
status_label.Text = "Restarting in " + answer;
}
}
当“seconds”变量的值代表一天或更短时间时,这非常有效,但当它超过 24 小时时,它只显示 24 小时的状态。我做错了什么?
(问题):
最佳答案
所有不以 Total
开头的属性仅包含不适合下一个更高属性的剩余部分。
换句话说:Days
属性将包含值 1
。
关于C# - TimeSpan 不适用于超过一天的秒数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18699854/