TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

ProgressBar Example: Windows Forms

This C# article examines ProgressBar in Windows Forms. It uses ProgressBar in a program with a background thread.

ProgressBar indicates visually the progress of an operation.

It is best used on a long-running computation or task. And the BackgroundWorker is often used to perform that task—it does not block the interface.

Example. Please add a ProgressBar control to your Windows Forms program by double-clicking on the ProgressBar entry in the Toolbox. For this example, we also need a BackgroundWorker—add one and then create the event handlers required.

BackgroundWorker

Note: We need the Load event handler, the DoWork event handler, and the ProgressChanged event handler.

Example that uses ProgressBar and BackgroundWorker: C#

using System.ComponentModel;
using System.Threading;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
	public Form1()
	{
	    InitializeComponent();
	}

	private void Form1_Load(object sender, System.EventArgs e)
	{
	    // Start the BackgroundWorker.
	    backgroundWorker1.RunWorkerAsync();
	}

	private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
	{
	    for (int i = 1; i <= 100; i++)
	    {
		// Wait 100 milliseconds.
		Thread.Sleep(100);
		// Report progress.
		backgroundWorker1.ReportProgress(i);
	    }
	}

	private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
	{
	    // Change the value of the ProgressBar to the BackgroundWorker progress.
	    progressBar1.Value = e.ProgressPercentage;
	    // Set the text.
	    this.Text = e.ProgressPercentage.ToString();
	}
    }
}

Starting the background task. In the Form1_Load event handler, please add a method call to the RunWorkerAsync method on the BackgroundWorker instance. This will create a thread and the DoWork method will begin.

Inside the DoWork event handler, we loop over the numbers 1 through 100. At each iteration, we wait one-tenth of a second. Then, we call the ReportProgress method on the BackgroundWorker instance.

And: In ProgressChanged, we set the value of the ProgressBar to the ProgressPercentage property of the ProgressChangedEventArgs argument.

Note: This will be the values 1 through 100. We also update the title text in the method.

Color. One of the useful properties on the ProgressBar is the ForeColor property. This can be set to a Color value to change the color of the bar itself that is drawn on the screen. It is often better to use the default color.

Note: In this screenshot, we see a yellow bar instead of the default blue bar. This is likely a bad design decision.

 

Summary. The ProgressBar control is often used in conjunction with a threading mechanism such as BackgroundWorker. With its many options, the ProgressBar makes a suitable widget for letting your users know the application is still active.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf