Progress Bar Series (Part 2) – Handling Progress with Notion of Completion in a GUI Application

This article is Part 2 in our Progress Bar series. In this article, we cover handling a script’s task progress with the notion of completion by using a “Progress Bar” control in a GUI application to provide a visual presentation of what is processing.

Backup Files GUI Scenario

In this scenario, we will develop a GUI application that creates a backup folder that will copy a series of files from a selected folder. This application will include a progress bar to visually show the progress while copying the files.

This application will prompt the user to select the location of the folder containing all text files to be backed up, and it will automatically create the backup folder in the selected folder. “Progress Bar” and “RichTextBox” controls have been added to the form to show what is going on during the process.

The combination of using both controls gives us the notion of completion in our GUI application.

“Select Folder” Button Control

To make the progress bar work, we added the necessary code behind the “Select folder” button control event.  The GUI application will prompt to select the folder containing the files to backup before starting the backup step. These files will be copied to a predefined backup folder-name in the chosen folder location.

After selecting the folder, we save the total number of files.

#Get all the text files in the folder
$files = Get-ChildItem $selectedPath -Filter *.txt;

Next, we use the saved total files to initialize the progress bar Maximum property. Then we initialize all other properties before starting to display the progress activity.

#Initialize the Progress Bar
$progressbar1.Maximum = $files.Count;
$progressbar1.Step = 1;
$progressbar1.Value = 0;

The progress bar will start with Value property = 0, and use the Step property to increment that Value by 1 when executing the for-each loop. The progress bar will start to increment.

#Copy the files and update the progress bar
$cnt = 1;
foreach ($file in $files)
Copy-Item ('{0}\\{1}' -f $selectedPath, $file) -Destination $destination;
$copiedfiles = "$cnt - $($ - copied from $($selectedPath) to $($destination).";
$richtextbox1.AppendText(($copiedfiles | Out-String -Width 1000));

## - Progress bar method to increment the slider:


Inside the foreach(..) loop logic, every time the PerformStep() method executes after the copy step the progress bar will increment by 1, as defined in the Step property.

While the GUI application is executing, we can see the progress bar increment as the files are copied to the selected backup folder. At the same time, we can see the information being updated in the RichTextBox area:


All of this done is done with just a few lines of code.


To make the progress bar work, we need to initialize the Maximum value to the total amount of the files being copied. Then we use the PerformStep() method to increment the progress bar.

Below is a general view of the logic used in the progress bar sample:

Download Progress Bar sample form:

What’s next?

Next up is Part 3 where we will demonstrate the use of the “Progress Bar Overlay” control.

Progress Bar series topics:

Related Articles


As always, if you have any ideas, comments, or feedback, please visit our feedback forum and reference this post.