C#Windows窗体倒计时计时器
我有3个文本框(小时、分钟、秒),一个开始、暂停和停止按钮,我用标签显示当前计时器。我还有一个间隔为C#Windows窗体倒计时计时器,c#,winforms,timer,countdown,C#,Winforms,Timer,Countdown,我有3个文本框(小时、分钟、秒),一个开始、暂停和停止按钮,我用标签显示当前计时器。我还有一个间隔为1000的计时器。我的问题是为什么不显示使用标签的时间?其他一切都正常,只是我没有在文本框中输入值 代码启动计时器: private void startbutton_Click(object sender, EventArgs e) { if (paused != true) //Timer is not paused (active) {
1000
的计时器。我的问题是为什么不显示使用标签的时间?其他一切都正常,只是我没有在文本框中输入值
代码启动计时器:
private void startbutton_Click(object sender, EventArgs e)
{
if (paused != true) //Timer is not paused (active)
{
int.TryParse(textBoxhrs.Text, out hours);
int.TryParse(textBoxmin.Text, out minutes);
int.TryParse(textBoxsec.Text, out seconds);
if (hours >= 1 || (minutes >= 1) || (seconds >= 1))
//If there is at least one integer entered in any of the 3 boxes, executes; else - //throws an exception
{
startbutton.Enabled = true;
pausebutton.Enabled = true; //changed the value to 'true'
stopbutton.Enabled = true; //changed the value to 'true'
textBoxhrs.Enabled = false;
textBoxmin.Enabled = false;
textBoxsec.Enabled = false;
}
else
{
MessageBox.Show("Enter at least one integer!");
}
}
}
private void stopbutton_Click(object sender, EventArgs e)
{
// Stop the timer.
paused = false;
timer1.Enabled = false;
startbutton.Enabled = true; //changed to true
stopbutton.Enabled = false; //changed to false
pausebutton.Enabled = false; //changed to false
textBoxsec.Clear();
textBoxmin.Clear();
textBoxhrs.Clear();
textBoxhrs.Enabled = true;
textBoxsec.Enabled = true;
textBoxmin.Enabled = true;
textBoxhrs.Enabled = true;
lblHr1.Text = "00";
lblMin1.Text = "00";
lblSec1.Text = "00";
MessageBox.Show("Timer is Stopped, to re-start press <Start>"); //updated to give user a chance to run the timer again after stoppage.
}
计时器:
private void timer1_Tick(object sender, EventArgs e)
{
// Verify if the time didn't pass.
if ((minutes == 0) && (hours == 0) && (seconds == 0))
{
// If the time is over, clear all settings and fields.
// Also, show the message, notifying that the time is over.
timer1.Enabled = false;
MessageBox.Show(textBoxMsg.Text);
pausebutton.Enabled = false;
stopbutton.Enabled = false;
startbutton.Enabled = true;
textBoxMsg.Clear();
textBoxsec.Clear();
textBoxmin.Clear();
textBoxhrs.Enabled = true;
textBoxMsg.Enabled = true;
textBoxsec.Enabled = true;
textBoxmin.Enabled = true;
textBoxhrs.Enabled = true;
lblHr1.Text = "00";
lblMin1.Text = "00";
lblSec1.Text = "00";
}
else
{
// Else continue counting.
if (seconds < 1)
{
seconds = 59;
if (minutes == 0)
{
minutes = 59;
if (hours != 0)
hours -= 1;
}
else
{
minutes -= 1;
}
}
else
seconds -= 1;
// Display the current values of hours, minutes and seconds in
// the corresponding fields.
lblHr1.Text = hours.ToString();
lblMin1.Text = minutes.ToString();
lblSec1.Text = seconds.ToString();
}
}
private void timer1\u勾选(对象发送方,事件参数e)
{
//确认时间是否没有过去。
如果((分钟=0)和&(小时=0)和&(秒=0))
{
//如果时间已过,请清除所有设置和字段。
//另外,显示消息,通知时间已过。
timer1.Enabled=false;
MessageBox.Show(textBoxMsg.Text);
pausebutton.Enabled=false;
stopbutton.Enabled=false;
startbutton.Enabled=true;
textBoxMsg.Clear();
textBoxsec.Clear();
textBoxmin.Clear();
textBoxhrs.Enabled=true;
textBoxMsg.Enabled=true;
textBoxsec.Enabled=true;
textBoxmin.Enabled=true;
textBoxhrs.Enabled=true;
lblHr1.Text=“00”;
lblMin1.Text=“00”;
lblSec1.Text=“00”;
}
其他的
{
//否则继续计数。
如果(秒<1)
{
秒=59;
如果(分钟==0)
{
分钟=59;
如果(小时数!=0)
小时数-=1;
}
其他的
{
分钟-=1;
}
}
其他的
秒-=1;
//在中显示小时、分钟和秒的当前值
//相应的字段。
lblHr1.Text=hours.ToString();
lblMin1.Text=minutes.ToString();
lblSec1.Text=seconds.ToString();
}
}
代码还有其他问题,但您没有看到标签更新的原因是您从未真正启动计时器
在“开始”对话框中,单击需要添加的内容:
...
stopbutton.Enabled = true; //changed the value to 'true'
textBoxhrs.Enabled = false;
textBoxmin.Enabled = false;
textBoxsec.Enabled = false;
timer1.Start(); //<-- start the timer object
。。。
stopbutton.Enabled=true//将值更改为“true”
textBoxhrs.Enabled=false;
textBoxmin.Enabled=false;
textBoxsec.Enabled=false;
timer1.Start()// 代码还存在其他问题,但您没有看到标签更新的原因是因为您从未真正启动计时器
在“开始”对话框中,单击需要添加的内容:
...
stopbutton.Enabled = true; //changed the value to 'true'
textBoxhrs.Enabled = false;
textBoxmin.Enabled = false;
textBoxsec.Enabled = false;
timer1.Start(); //<-- start the timer object
。。。
stopbutton.Enabled=true//将值更改为“true”
textBoxhrs.Enabled=false;
textBoxmin.Enabled=false;
textBoxsec.Enabled=false;
timer1.Start()// 您需要做两件事才能使其正常工作:
您需要在开始按钮中启动计时器,单击:
if (hours >= 1 || (minutes >= 1) || (seconds >= 1))
//If there is at least one integer entered in any of the 3 boxes, executes; else - //throws an exception
{
startbutton.Enabled = true;
...
timer1.Enabled = true;
}
您需要将timer Tick事件连接到timer1\u Tick。您可以通过选择计时器并单击“属性”框上的“闪电”工具栏图标,然后选择“计时器1”来完成此操作。要使其正常工作,您需要做两件事:
您需要在开始按钮中启动计时器,单击:
if (hours >= 1 || (minutes >= 1) || (seconds >= 1))
//If there is at least one integer entered in any of the 3 boxes, executes; else - //throws an exception
{
startbutton.Enabled = true;
...
timer1.Enabled = true;
}
您需要将timer Tick事件连接到timer1\u Tick。您可以通过选择计时器并单击“属性”框架上的“闪电”工具栏图标,然后选择“计时器1_Tick for Tick”来执行此操作。如何执行此操作?编辑:谢谢:D完成:)点击答案下方的绿色勾选框。如果他们真的回答了问题,你只需要这样做,但不管你问的问题有多远,你都值得再问一遍。编辑:刚刚意识到其中一个答案是我:)ThanksI建议使用数字向上向下(又名旋转按钮)。它不允许非法输入,您可以设置最大值。阅读更多:我如何做到这一点?编辑:谢谢:D完成:)点击答案下方的绿色勾选框。如果他们真的回答了问题,你只需要这样做,但不管你问的问题有多远,你都值得再问一遍。编辑:刚刚意识到其中一个答案是我:)ThanksI建议使用数字向上向下(又名旋转按钮)。它不允许非法输入,您可以设置最大值。阅读更多:或者您可以使用timer1.Enabled=true。但是,使用timer.Stop()和timer.Start()会更清楚,而不是使用timer.Enabled=true或false(这会使事情变得相同,但可读性更强),或者可以使用timer1.Enabled=true。但是,使用timer.Stop()和timer.Start()会更清楚,而不是使用timer.Enabled=true或false(这会使事情变得相同,但可读性更强),谢谢!这工作正常,我将它与timer.Start()和stop代码相结合,现在更好了!谢谢这工作正常,我将它与timer.Start()和stop代码相结合,现在更好了!