我想根据用户输入让我的计时器倒计时。
这就是我的表格:
这是我的代码:
private int counter=80;
DateTime dt = new DateTime();
private void button1_Click(object sender, EventArgs e)
{
progressBar1.Maximum = counter * 1000;
progressBar1.Step = 1000;
timer1 = new System.Windows.Forms.Timer();
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Interval = 1000;
timer1.Start();
textBox1.Text = counter.ToString();
}
private void timer1_Tick(object sender, EventArgs e)
{
counter--;
progressBar1.PerformStep();
if (counter == 0)
{
timer1.Stop();
}
textBox1.Text = dt.AddSeconds(counter).ToString("mm:ss");
}
private void Form1_Load(object sender, EventArgs e)
{
timer1.Enabled = false;
}
private void button2_Click(object sender, EventArgs e)
{
timer1.Stop();
textBox1.Clear();
progressBar1.Value = 0;
}
如果用户在文本框中输入新值,我该如何更新计时器?
答案 0 :(得分:1)
将OnTextChanged事件处理程序附加到textbox1。当textinput改变时停止计时器
protect void textinput1_OnTextChange(object sender, EventArg e) {
button2_Click(sender, e);
}
或者您可以在计时器启动时禁用用户输入,并在计时器停止后重新启用它。
答案 1 :(得分:0)
只需在单击开始按钮时将启用文本框的值设置为false即可。并在计时器停止时再次启用它或在取消按钮上选中。这是一个代码:
textBox1.Enabled = false;
而
textBox1.Enabled = true;