Start button / pause

Asked

Viewed 47 times

0

I have a code that makes a stopwatch. I currently have 1 button to start time and another to continue time after having stopped. If you click the start it starts again.

how can I only have 1 button to start and pause?

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;

namespace WindowsFormsApp3
{
    public partial class Form1 : Form
    {
        private int totalSeconds;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            for(int i = 0; i < 60; i++)
            {
                this.comboBox1.Items.Add(i.ToString());
                this.comboBox2.Items.Add(i.ToString());

            }
            this.comboBox1.SelectedIndex = 30;
            this.comboBox2.SelectedIndex = 59;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int minutes = int.Parse(this.comboBox1.SelectedItem.ToString());
            int seconds = int.Parse(this.comboBox2.SelectedItem.ToString());
            totalSeconds = (minutes * 60) + seconds;
            this.timer1.Enabled = true;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.timer1.Stop();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (totalSeconds > 0)
            {
                totalSeconds--;
                int minutes = totalSeconds / 60;
                int seconds = totalSeconds - (minutes * 60);
                this.label3.Text = minutes.ToString() + ":" + seconds.ToString();

            }
            else
            {
                this.timer1.Stop();
                MessageBox.Show("Tempo Acabou!");
            }

        }

        private void button3_Click(object sender, EventArgs e)
        {
            totalSeconds = totalSeconds + 60;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.timer1.Start();
        }
    }
}
  • someone please?

  • so that so much hurry to correct in blocking the posts and there is no one who helps?

1 answer

1


I do not recommend using the Timer for this operation, since the Timer is an event repeater after a certain period, not a chronometer. But the . NET has a chronometer class, called System.Diagnostics.Stopwatch, but it does not have the same function as a Timer, as well as vice versa.

using System.Diagnostics;
...
public Stopwatch contador = new Stopwatch();
private void button2_Click(object sender, EventArgs e)
{
    if (contador.IsRunning) {
        contador.Stop();
    } else {
        contador.Start();
    }
}

The method Start starts or resumes measuring the elapsed time of an interval. If you want to start measuring time again, use the method Restart.

  • The Restart will reset the counter and will immediately start a new count.
  • The method Reset will only reset the counter, but will not start the new count.
  • The StartNew performs the same function as Restart but returning a new instance of a Stopwatch.

Documentation of Stopwatch.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.