sd_dracula sd_dracula - 10 months ago 113
C# Question

c# progress bar percentage

I have a progress bar in a winform c# application and I sue that as a progress indicator.
The progress bar can have a different Maximum size depending on the amount of user input (which can go over 100) so this is how I set it up:

this.pbLoadingWrite.Maximum = Input.Length;
this.pbLoadingWrite.Step = 1;

then just update the progress bar with:


All works fine but I would like to display a % number on top of the progress bar for better visibility.

Since the Input.Length can be > 100, what's the syntax for displaying the percentage? Are there any helper functions built into VS c#?

Answer Source

Calculating the percentage is quite easy in this case

int percent = (progressbar1.Value / progressbar.Maximum) * 100;

Break down:

value = 56, Max = 300,

56 / 300 = 0.186666
0.186666 * 100 = 18.666%