Timer Countdown with progress bar C#

Viewed 6585

I want to sync my progress bar in my project with the timer countdown.

This is what I has now:

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)
        {
            int counter = 80;
            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.Increment(1);
            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;
        }
    }
}

I want the progress bar to finish when the timer to ends.

enter image description here

1 Answers
Related