Click here to Skip to main content
15,889,861 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Please help me,

i want to sync my progress bar in my project with the timer countdown,
i want when the timer is end, the progress bar is finish too

this is what i has now,

What I have tried:

C#
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;
using System.Diagnostics;

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;
        }
    }
}
Posted
Updated 24-Mar-16 23:19pm
v2
Comments
[no name] 23-Mar-16 13:47pm    
a.) Pay Attention what happens if you press button1_Click more than one time...you should fix this also.
b.) Set ProgressBar's Maximum to your Counter value (which you have declared twice, you should also fix this).
OriginalGriff 23-Mar-16 15:09pm    
I'd suggest you post that as a solution - it's the right approach!
[no name] 23-Mar-16 15:47pm    
Thank you for your encouragement. I just need to work "in" China, the systems are now free for me. Long nigth in front of me :(
Bruno
FARONO 24-Mar-16 3:27am    
Also beware in the Eventhandler method, counter--; could go below 0.
ZurdoDev 24-Mar-16 8:04am    
And what is your question?

1 solution

Just Try my code... any doubt ask me in fb.com/syedmca


C#
public partial class Form1 : Form
   {
       public Form1()
       {
           InitializeComponent();
           timer1.Start();
           timer1.Interval = 100;

       }

       private void timer1_Tick(object sender, EventArgs e)
       {
           if (label1.Text == "0")
           {
               timer1.Stop();
           }
           else
           {
               label1.Text = (Convert.ToInt32(label1.Text) - 1).ToString();
               progressBar1.Value = 100-Convert.ToInt32(label1.Text);

           }



       }


   }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900