小编yuk*_*iko的帖子

计时器和按钮问题

我正在使用C#使用Visual Studio 2013进行项目.我希望计时器从120分钟倒计时到0分钟,当它达到0时,一个消息框将显示时间已到.我还希望在按下重置按钮时重置计时器.但是,当我按下重置按钮时,即使定时器被重置,我按下开始按钮,同时出现消息框"Time up up",定时器也不会启动.请帮我.我是C#的新手,所以请尽量不要给我很难理解的复杂答案.此外,你会看到我更多,因为我会有更多问题要问.谢谢!!(这是我的代码.)

    private void startbutton_Click(object sender, EventArgs e)
    {
        timer.Enabled = true;
        foreach (var button in Controls.OfType<Button>())
        {
            button.Click += button_Click;
            timer.Start();
            button.BackColor = Color.DeepSkyBlue;
        }
    }

    private void stopandresetbutton_Click(object sender, EventArgs e)
    {
        button.BackColor = default(Color);
        timer.Stop();
        timeleft = 0;
        timerlabel.Hide();
    }

    int timeleft = 120;
    private void timer_Tick(object sender, EventArgs e)
    {
        if (timeleft > -1)
        {
            timerlabel.Text = timeleft + " minutes";
            timeleft = timeleft - 1;
        }
        else
        {
            timer.Stop();
            MessageBox.Show("Time's up!");
        } …
Run Code Online (Sandbox Code Playgroud)

c# timer button

5
推荐指数
1
解决办法
124
查看次数

标签 统计

button ×1

c# ×1

timer ×1