如何让定时器重新启动?

发布于 2025-01-05 18:41:43 字数 921 浏览 1 评论 0原文

我有从文件读取并通过标签流式传输的代码。但是当我取消选中该复选框并再次选中它时,它只是从它停止的位置开始,

这是我的代码

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

黄昏下泛黄的笔记 2025-01-12 18:41:43

设置 INTERVAL 属性...以毫秒为单位...1000 = 1 秒

set the INTERVAL property... in milliseconds... 1000 = 1 second

云淡风轻 2025-01-12 18:41:43

Timer.EnabledTimer.Start() 在功能上是等效的。

我怀疑,您需要重置开始变量。

if (checkBox1.Checked == true)
{
  start = 0;
  label1.Text = string.Empty;
  timer1.Start();
}
else
{
  timer1.Stop();
}

编辑以包括重置每个评论的标签。

Timer.Enabled and Timer.Start() are functionally equivalent.

I suspect, you'll need to reset your start variable.

if (checkBox1.Checked == true)
{
  start = 0;
  label1.Text = string.Empty;
  timer1.Start();
}
else
{
  timer1.Stop();
}

Edit to include the resetting of the label per comment.

眼眸 2025-01-12 18:41:43

如果您在检查更改事件上将开始重置为零,则应该重置计时器:

private void checkBox1_CheckedChanged(object sender, EventArgs e)
    {
        if (checkBox1.Checked == true)
        {
            start = 0;
            label1.Text = string.Empty;
            timer1.Enabled = true;
            timer1.Start();
        }
        else
        {
            timer1.Enabled = false;
        }
    }

我有一个类似的过程,其中我在加载表单时启动计时器,然后如果他们有一个表单打开一段时间,我会给他们保持表单打开的选项。如果他们这样做,那么我将计时器计数重置为零,然后该过程重新开始。

If you reset your start to zero on the check change event, that should reset the timer:

private void checkBox1_CheckedChanged(object sender, EventArgs e)
    {
        if (checkBox1.Checked == true)
        {
            start = 0;
            label1.Text = string.Empty;
            timer1.Enabled = true;
            timer1.Start();
        }
        else
        {
            timer1.Enabled = false;
        }
    }

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文