用户输入的C#win frm计时器倒计时 [英] C# win frm timer countdown with user input

查看:85
本文介绍了用户输入的C#win frm计时器倒计时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助我,

i会在textBox1中为我的计时器倒计时输入一个用户输入,



我尝试了什么:



这是我的代码:





使用System;

使用System.Collections.Generic;

使用System.ComponentModel;

使用System.Data;

使用System.Drawing;

使用System.Linq;

使用System.Text;

使用System.Threading.Tasks;

使用System.Windows.Forms;

使用System.Diagnostics;



名称空间Timer1

{

公共部分课程表格1:表格

{

public Form1()

{

InitializeComponent();

}



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发件人,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;

}



private void textBox1_TextChanged(object sender,EventArgs e)

{

button2_Click(sender,e);

}

}

}

please help me,
i would have a user input in textBox1 for my timer countdown,

What I have tried:

this is my code:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;

namespace Timer1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

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;
}

private void textBox1_TextChanged(object sender, EventArgs e)
{
button2_Click(sender, e);
}
}
}

推荐答案

嗯...这是一些奇怪的代码你去那里...

看看你在做什么:

当你的文字发生变化时,你将TextBox(作为发送者)传递给需要按钮的处理程序。 ..which清除导致原始事件处理程序被调用的文本框,导致它再次发生...并停止计时器。而且由于你的Tick事件改变了文本框中的文本 - 导致TextChanged事件 - 所有会发生的事情(最多)是一个Tick和一个空白文本框...



粗暴地说,那段代码看起来像是在猜测并希望它能起作用 - 无论你想做什么,我都不能从那个代码开始知道 - 并且希望和祈祷没有。在发展中工作。停止。

回到开始,然后决定你想做什么。抛弃那些代码,然后看看需要发生什么才能让它按照你想要的方式工作。

如果你想把倒计时(或计数)计时器放到一个文本框中,那就是琐碎的,但只有当你考虑需要发生什么而不仅仅是尝试并希望它们能够发挥作用时!
Um...that's some odd code you go there...
Look at what you are doing:
When your text changes, you pass the TextBox (as sender) to a handler that expects a button...which clears the textbox that causes the original event handler to be called, causing it to occur again...and stops the Timer. And since your Tick event changes the text in the textbox - causing the TextChanged event - all that will happen is (at best) a single Tick and a blank text box...

To be brutally honest, that code looks like you are guessing and hoping it will work - whatever it is you are trying to do and I can't begin to know from that code - and "hope and pray" doesn;t work in development. Stop.
Go back to the beginning, and decide what you want to do. Throw that code away, and look at what needs to happen in order for it work work as you want.
If you are trying to put a countdown (or count up) timer into a text box, that's trivial, but only if you think about what needs to happen instead of just trying things and hoping they will work!


这篇关于用户输入的C#win frm计时器倒计时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆