Progress bar in C#

January 2017



Issue


I am creating a program which runs a parameterized query to add 14,370 records to a table. The downside is that it takes about 15 seconds. I would use a progressbar to show the user the progress of the execution of the query. I need an example.

Solution


Create a winform with a GroupBox1, a label and a button that is outside the GroupBox.
  • Put the Text property of the GroupBox in an empty string.
  • Put the property of AutoSize to False, the color of the label to Lime for example.
  • The Size property is set to 0, 15
  • Code:


int counter=0;
int rowMax=2000;
int colMax=2000;
decimal pcdDone;
for(int r=0;r<rowMax;r++)
{
for(int c=0;c<colMax;c++)
{
counter++;
}
pcdDone=counter/(rowMax*colMax);
groupBox1.Text=((int)(pcdDone*100)).ToString() +"%";
groupBox1.refresh();
label1.with=Convert.ToInt32(pcdDone*(groupBox1.winth-10));
}

Thanks to OUROBANGNA for this tip.

Related


Published by aakai1056. Latest update on May 29, 2012 at 12:22 PM by aakai1056.
This document, titled "Progress bar in C#," is available under the Creative Commons license. Any copy, reuse, or modification of the content should be sufficiently credited to CCM (ccm.net).