sd_dracula
sd_dracula

Reputation: 3896

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:

this.pbLoadingWrite.PerformStep(); 

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#?

Upvotes: 2

Views: 28121

Answers (1)

sa_ddam213
sa_ddam213

Reputation: 43596

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%

Upvotes: 5

Related Questions