C# ProgressBar and Countdown Timer - START,STOP,RESET

June 11, 2017 Oclemy C# Timer, C# ProgressBar 0 minutes, 50 seconds

This is a C# progressbar and timer example.We Start,Stop and Reset our timer and progressbar.Meanwhile we update the progressbar and percentage label showing the current progress value. Here's the code :  

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 Splasher
{
    public partial class Form1 : Form
    {

        int progress = 0;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //INCREMENT OUR PROGRESS
            progress += 1;
            if(progress >= 100)
            {
                timer1.Enabled = false;
                timer1.Stop();
            }
            //OTHERWISE
            progressBar1.Value = progress;
            percentTxt.Text = progress.ToString()+" .00 %";
        }

        private void startBtn_Click(object sender, EventArgs e)
        {
            //ENABLE AND SET INTERVALL
            timer1.Enabled = true;
            timer1.Interval = 50;
        }

        private void stopBtn_Click(object sender, EventArgs e)
        {
            //STOP
            timer1.Stop();
            //UPDATE BUTTON TXT
            if(progress>0 && progress<100)
            {
                startBtn.Text = "Resume";
            }
        }

        private void resetBtn_Click(object sender, EventArgs e)
        {
           //RESETTING
            progress = 0;
            progressBar1.Value = 0;
            percentTxt.Text = "0.00";
        }
    }
}

Best regards.

Comments