Click here to Skip to main content
15,885,214 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi
I'm writing an application where i need to show a progressbar, below is a sample of the code. Problem: the percentage variable does not get updated, it's value is always zero, any ideas why?

C#
private void trTick_Tick(object sender, EventArgs e)
        {
            pbProg.Minimum = 0;
            pbProg.Maximum = 100;
            pbProg.Value = 1;
            if (pbProg.Value == pbProg.Maximum)
            {
                //trTick.Stop();
                trTick.Enabled = false;
                pbProg.Value = 0;
                pbProg.Enabled = false;
            }
            pbProg.Step = 1;
            pbProg.PerformStep();
            pbProg.Value++;
            int percentage = (pbProg.Value / pbProg.Maximum) * 100;
            lblProg.Text = "Current progress: " + percentage.ToString() + "%";

        }
private void btnStart_Click(object sender, EventArgs e)
      {
          trTick.Enabled = true;

      }
Posted
Comments
Reiss 15-Jul-11 10:55am    
Have you started the Timer - this is different to just enabling it?
If yes, have you stepped through the trTick_Tick method?
tlhogi.mmusi 15-Jul-11 11:19am    
Reiss,

yes i did, actually the progress is fine. The problem (well?) is that int percentage is not getting set even though i can see what the .Maximum and the .Value are?

int percentage = (pbProg.Value / pbProg.Maximum) * 100;

You're getting integer division here, so the first part ends up as 0. Try:
int percentage = (pbProg.Value * 100) / pbProg.Maximum;
 
Share this answer
 
You have to call pbProg.Update().
 
Share this answer
 
v2
The timer is not necessarily required. You can do this in different ways. You can use a for loop to increase the value of the progress bar.


C#
#region Using directives
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
namespace Progress_Bar_Example
{
    partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            
            //Set the Max. value the progress bar can hold to 100.

            progressBar1.Maximum = 100;
        
        }
        private void button1_Click(object sender, EventArgs e)
        {
            int value;
            for (value = 0; value != 1000; value++)
            {
                progressBar1.Value = progressBar1.Value + 1;
            }
  

        }
    }
}


This may not be the perfect way of doing it but still it works fine. There may be many other solutions. Let me know if this was helpful

--
AJ
 
Share this answer
 
Comments
Tarun.K.S 15-Jul-11 13:47pm    
What about displaying the progress?
Hi AJ

Thanks for your solution. The problem is actually here:
MIDL
int percentage = (pbProg.Value / pbProg.Maximum) * 100;
          lblProg.Text = "Current progress: " + percentage.ToString() + "%";


The progressbar actually works (obviously not 100%) but while stepping through the code,
I can see what the current values for pbProg.Value and pbProg.Maximum are but "int percentage" is always zero. Can you suggest a different way of getting the percentage?
 
Share this answer
 
Comments
ankitjoshi24 15-Jul-11 13:25pm    
My solution does provide the way to see from 0 to 100%

Just try the solution (if you have not) coz I think it will show the percentage of the progress bar.
The "percentage" is represented by the pbProg.Value property. A progressbar shows 0-100% (that's why it's a "progress" bar). You're doing math on it that doesn't need to be done.
 
Share this answer
 
Assuming that trTick_Tick is correctly called by your timer, for sure your progress bar won't ever increment as you reset its value to 1 each time you enter in that function.

At most, the value would be 3 when you compute the percentage and as pointed out by someone else, it would help to do the multiplication before the division since you are using an integer type.

By the way, it would be fairly easy to see the problem by using the integrated debugger in Visual Studio and tracing that code line per line.
 
Share this answer
 
v2
Hello, try the following... I've seen int have this problem before...

double percentage = ((double)value / (double)max) * 100);


If your initial thought on reading this is 'that can't be it'... try this simple console test...

int val = 5;
int total = 100;
int pct = (val / total) * 100;

Console.WriteLine(pct);



pct always zero... make everything double and hey... dar it goes.
 
Share this answer
 
v3
You have to avoid multiplying by 0, just start with the multiply then division ... this should be work fine.
<pre lang="cs">int percentage = (pbProg.Value * 100) / pbProg.Maximum;



Good luck :).
 
Share this answer
 
This worked:
C#
double percentage = ((double)value / (double)max) * 100);


Thanks
 
Share this answer
 

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