我认为这个问题的标题不好,但我找不到更好的。
我想要做的是让用户能够设置倒数计时器。(看图1):
应用程序接受用户输入并检查用户选择了哪个时间单位,然后将其转换为秒并将值分配给一个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;
}
}
当“秒”变量的值表示一天或更少时,这非常有效,但是当它超过 24 小时时,它只显示状态中的 24 小时。我究竟做错了什么?
( 问题 ):