如何让定时器重新启动?
我有从文件读取并通过标签流式传输的代码。但是当我取消选中该复选框并再次选中它时,它只是从它停止的位置开始,
这是我的代码
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked == true)
{
timer1.Enabled = true;
timer1.Start();
}
else
{
timer1.Enabled = false;
}
}
private int start = 0;
private string lyricspath = @"lyrics.txt";
private void timer1_Tick(object sender, EventArgs e)
{
TextReader reader = new StreamReader(lyricspath);
string[] read = File.ReadAllLines(lyricspath);
string join = string.Join(" ", read);
int number = join.Length;
start++;
string str = join.Substring(start, 15);
if (start == number - 15)
{
start = 0;
}
label1.Text = str;
}
,有什么方法可以让计时器在每次取消选中然后选中该复选框时完全启动?
i have code that reads from a file and streams it through a label. but when i uncheck the checkbox and check it again it just picks up from where it left off
this is my code
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked == true)
{
timer1.Enabled = true;
timer1.Start();
}
else
{
timer1.Enabled = false;
}
}
private int start = 0;
private string lyricspath = @"lyrics.txt";
private void timer1_Tick(object sender, EventArgs e)
{
TextReader reader = new StreamReader(lyricspath);
string[] read = File.ReadAllLines(lyricspath);
string join = string.Join(" ", read);
int number = join.Length;
start++;
string str = join.Substring(start, 15);
if (start == number - 15)
{
start = 0;
}
label1.Text = str;
}
is there any way to make the timer start completely over each time i uncheck then check the checkbox?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
设置 INTERVAL 属性...以毫秒为单位...1000 = 1 秒
set the INTERVAL property... in milliseconds... 1000 = 1 second
Timer.Enabled
和Timer.Start()
在功能上是等效的。我怀疑,您需要重置开始变量。
编辑以包括重置每个评论的标签。
Timer.Enabled
andTimer.Start()
are functionally equivalent.I suspect, you'll need to reset your start variable.
Edit to include the resetting of the label per comment.
如果您在检查更改事件上将开始重置为零,则应该重置计时器:
我有一个类似的过程,其中我在加载表单时启动计时器,然后如果他们有一个表单打开一段时间,我会给他们保持表单打开的选项。如果他们这样做,那么我将计时器计数重置为零,然后该过程重新开始。
If you reset your start to zero on the check change event, that should reset the timer:
I have a similar process in which I start a timer when a form loads, then if they have a form open for a period of time I give them the option to keep the form open. If they do, then I reset the timer count to zero and the process starts over.