Code For Progress Bar In Windows Application Using C#.NET

This article will teach you how you can show the progress bar in Windows applications, using C#.NET. So, for this application, first we will create a new Windows application and add a progress bar control.
 
 
 
Now, we will assign the maximum value to the progress bar control. This will help to show the progress.
 
 
 
After this control setting, we will add a backgroundWorker control.
 
 
 
Now, generate the  DoWork and ProgressChanged event for the control backgroundWorker.
 
 

After this, we will add the code on DoWork event.
  1. private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)  
  2.     {  
  3.         for (int i = 1; i <= 100; i++)  
  4.         {  
  5.         // Wait 50 milliseconds.  
  6.         Thread.Sleep(50);  
  7.         // Report progress.  
  8.         backgroundWorker1.ReportProgress(i);  
  9.         }  
  10.     }  
 Now, we will add the code for ProgressChanged.
  1. private void backgroundWorker1_ProgressChanged(object sender,  
  2.         ProgressChangedEventArgs e)  
  3.     {  
  4.         // Change the value of the ProgressBar   
  5.         progressBar1.Value = e.ProgressPercentage;  
  6.         // Set the text.  
  7.         this.Text = e.ProgressPercentage.ToString();  
  8.     }   
Now, we will add the code to invoke the backgroundWorker.
  1. private void Form2_Load(object sender, System.EventArgs e)  
  2. {  
  3.         backgroundWorker1.WorkerReportsProgress = true;
  4.         backgroundWorker1.RunWorkerAsync();  
  5. }  
So, here is the complete code of what we have done. 
  1. using System.ComponentModel;  
  2. using System.Threading;  
  3. using System.Windows.Forms;  
  4.   
  5. namespace WindowsFormsApplication1  
  6. {  
  7.     public partial class Form2 : Form  
  8.     {  
  9.     public Form2()  
  10.     {  
  11.         InitializeComponent();  
  12.     }  
  13.   
  14.     private void Form2_Load(object sender, System.EventArgs e)  
  15.     {  
  16.             backgroundWorker1.WorkerReportsProgress = true;
  17.             backgroundWorker1.RunWorkerAsync();  
  18.     }  
  19.   
  20.     private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)  
  21.     {  
  22.         for (int i = 1; i <= 100; i++)  
  23.         {  
  24.         // Wait 50 milliseconds.  
  25.         Thread.Sleep(50);  
  26.         // Report progress.  
  27.         backgroundWorker1.ReportProgress(i);  
  28.         }  
  29.     }  
  30.   
  31.     private void backgroundWorker1_ProgressChanged(object sender,  
  32.         ProgressChangedEventArgs e)  
  33.     {  
  34.         // Change the value of the ProgressBar  
  35.         progressBar1.Value = e.ProgressPercentage;  
  36.         // Set the text.  
  37.         this.Text =  "Progress: " + e.ProgressPercentage.ToString() + "%"; 
  38.     }  
  39.     }  
  40. }  
 Now, we are done. Run the application to check the output.
 
 


Similar Articles