How to Create a Progress Bar in C#
- Create a Progress Bar in Windows Forms App (.NET Framework)
-
Create a Progress Bar Using the
BackgroundWorker
Class inC#
-
Create a Progress Bar With Text in
C#
Lengthy file operations and calculations require indications of progress, and a progress bar is the best way to display progress information to the user during a long-running operation. This tutorial will teach you how to create three different progress bars in C# using Visual Studio 2022.
A dynamic progress bar in C# features two properties to specify its range (Maximum
and Minimum
properties), mainly equivalent to the number of files to copy or processes to run. Furthermore, when a process is complete and to perform increment in the value of the progress bar, the Step
property with the PerformStep
method is used.
A pre-designed progress bar in C# can only be oriented horizontally; however, you can create a custom progress bar to suit your needs. A progress bar’s primary purpose is to alert the user about the progress of currently performing tasks by a C# application.
Create a Progress Bar in Windows Forms App (.NET Framework)
A progress bar acts as a user interface element that indicates the progress of an operation. A progress bar in C# supports two modes representing progress: indeterminate and continuous.
The indeterminate mode for the progress bar comes in handy when performing a task or operation you don’t know how long it will take. It is done by loading data asynchronously on a background thread (generally with the help of the BackgroundWorker
class) while the C# application is still responsive to the user.
It’s a common design paradigm to display an indeterminate progress bar as data is loading and is set until actual progress begins. When creating a progress bar in WinForms, it’s important to set the progress bar’s Style
property to Marquee
.
However, a progress bar in WPM has the Isdeterminate
property which is similar to Style
, and its value as True
works similar to Marquee
.
// namespaces for C# windows application (.NET Framework)
using System;
using System.Windows.Forms;
namespace ProgressBarExp {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
// create a `progressBar1` progress bar in your C# project design
private void progressBar1_Click(object sender, EventArgs e) {}
// create a method that performs some calculations
// the progress bar will synch with this method and progress accordingly
private void Caluculate(int i) {
// perform a task
}
// create a `RunOperation` button to perform calculations or take action in your C# project
// design
private void RunOperation_Click(object sender, EventArgs e) {
// set the visibility of progress bar
progressBar1.Visible = true;
for (int j = 0; j < 10000001; j++) {
Caluculate(j);
if (j == 10000000) {
MessageBox.Show("Operation Complete!");
progressBar1.Visible = false;
}
}
}
private void Form1_Load(object sender, EventArgs e) {
// for indeterminate progress using .NET framework
progressBar1.Style = ProgressBarStyle.Marquee;
progressBar1.Visible = false;
}
}
}
Output:
A progress bar only serves its purpose correctly when it reports progress as your data is loaded. In C# applications, progress bars are popular for installing software, performing lengthy tasks, and startup, so the users know exactly how much time a process will take and how much progress is remaining.
To create a continuous progress bar in C#, remove the code from the RunOperation_Click
event and replace it with the following code:
private void RunOperation_Click(object sender, EventArgs e) {
progressBar1.Visible = true;
progressBar1.Maximum = 10000000;
progressBar1.Value = 1;
for (int j = 0; j < 10000001; j++) {
Caluculate(j);
if (progressBar1.Value == 10000000) {
MessageBox.Show("Operation Complete!");
progressBar1.Visible = false;
break;
}
// it will measure the progress of the task or operations and progress in regards to the
// completion of a task It only requires if the progress bar style is not set to Marquee
progressBar1.PerformStep();
}
}
Output:
In the Form1_Load
event, comment the progressBar1.Style = ProgressBarStyle.Marquee;
line from C# code, and you will have a continuous progress bar according to the task completion percentage %
.
Create a Progress Bar Using the BackgroundWorker
Class in C#
It must be clear how straightforward it is to use a progress bar in C# applications. Now it’s time to study how a progress bar moves forwards by synching with some actual work and not just by a static value.
Where most of the new C# developers run into trouble is when they realize that you can’t perform a task and update a progress bar, at the same time, on the same thread. If you are performing a heavy task on the user interface thread of a C# application and trying to update the progress bar accordingly, you require the services of a BackgroundWorker
class in C#.
Without the help of the BackgroundWorker
class, which pretty much renders a progress bar useless. The progress bar won’t show any update to the progress before a task is completed successfully and requires a background worker to update both on a single thread simultaneously.
It is an ideal way to create a progress bar with a background working in C# to perform a task on a worker thread and then push the update to the user interface thread, which will instantly update the progress bar and show these updates.
using System;
using System.Windows;
using System.Threading;
using System.Windows.Forms;
using System.ComponentModel;
namespace ProgressBarExp {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
// create a progress bar in your C# project's Form1.cs [Design]
private void progressBar1_Click(object sender, EventArgs e) {}
// a button to perform an operation synched with the `progressBar1`
private void RunOperation_Click(object sender, EventArgs e) {
BackgroundWorker taskSynch = new BackgroundWorker();
taskSynch.WorkerReportsProgress = true;
taskSynch.DoWork += taskSynch_DoWork;
taskSynch.ProgressChanged += taskSynch_ProgressChanged;
taskSynch.RunWorkerAsync();
}
// Form1 load event
private void Form1_Load(object sender, EventArgs e) {}
// create a method that connects `BackgroundWorker` with a task
void taskSynch_DoWork(object sender, DoWorkEventArgs e) {
for (int i = 0; i < 100; i++) {
(sender as BackgroundWorker).ReportProgress(i);
Thread.Sleep(100);
}
}
// create a method that connects a task with the `progressBar1`
void taskSynch_ProgressChanged(object sender, ProgressChangedEventArgs e) {
progressBar1.Value = e.ProgressPercentage;
}
}
}
Output:
Create a Progress Bar With Text in C#
Generally, you can create a custom progress bar that gives you more control over its configuration. However, writing a C# code from scratch to create a progress bar is lengthy work; you can use NuGet
packages like Syncfusion UI Control
or ShellProgressBar
to get ready-to-use progress bars with incredible designs.
The simplest way to create a progress bar with text is by using labels and changing their text according to the progression of a task. You can synch its progression with the progression of a task.
using System;
using System.Threading;
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing;
namespace ProgressBarExp {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
private void RunOperation_Click(object sender, EventArgs e) {
BackgroundWorker taskSynch = new BackgroundWorker();
taskSynch.WorkerReportsProgress = true;
taskSynch.DoWork += taskSynch_DoWork;
taskSynch.ProgressChanged += taskSynch_ProgressChanged;
taskSynch.RunWorkerAsync();
}
private void Form1_Load(object sender, EventArgs e) {
// make the percentage label `backcolor` transparent
label1.BackColor = Color.Transparent;
}
void taskSynch_DoWork(object sender, DoWorkEventArgs e) {
for (int i = 0; i < 101; i++) {
(sender as BackgroundWorker).ReportProgress(i);
Thread.Sleep(100);
// to support multi-threading for label1 and label 2
if (label1.InvokeRequired) {
label1.Invoke(new MethodInvoker(delegate {
// shows the progression, it is syched with the task progression
label1.Text = "Progress: " + i + "%";
}));
}
if (label2.InvokeRequired) {
label2.Invoke(new MethodInvoker(delegate {
if (i < 25) {
label2.Text = "Starting the task...";
} else if (i > 25 && i < 50) {
label2.Text = "Half way through...";
} else if (i > 50 && i < 99) {
label2.Text = "The wait will be over soon...";
} else if (i == 100) {
label2.Text = "Task is completed successfully!";
}
}));
}
}
}
void taskSynch_ProgressChanged(object sender, ProgressChangedEventArgs e) {
// it will update the progress bar in real time depends on the progress of a task
progressBar1.Value = e.ProgressPercentage;
}
}
}
Output:
You must create a progressBar1
progress bar, a Run Operation
button, and label1
and label2
labels in Form1 [Design]
of your C# project before running this C# code.
In this tutorial, you learned how to create different types of progress bars and synch them with the real progression of a task using the BackgroundWorker
class in C#.
Hassan is a Software Engineer with a well-developed set of programming skills. He uses his knowledge and writing capabilities to produce interesting-to-read technical articles.
GitHub