ProgressBar Control

The control of Windows Forms ProgressBar shows the progress of a process by displaying a suitable number of rectangles arranged in a horizontal bar. The bar is filled when the process is finished. Progress bars are commonly used to provide the user with an idea of how long to wait for a process to complete; for example, when loading a large file.

A value, minimum, and maximum are the key features of the Progressbar properties. Minimum and maximum characteristics set the highest and lowest values which can appear at the progress bar.

The value property shows the progress towards the completion of the operation. As the bar displayed in the control consists of blocks, the value that the ProgressBar control displays only approximates the current value of the Value property. The Value property determines when the next block is displayed based on the size of the ProgressBar control.

Example:

In the lodaing of large file. In kilobytes, we could set the maximum to the file size. If the maximum property is set to 100, the minimum property is set to 10, and the value is set to 50, the minimum property will show 5 rectangles. It is half the number that can be shown.

However,

Other methods to alter the ProgressBar control displayed value are available, The Step property can be used to set a value for the value property to be increased with. Then the method PerformStep increases the value.

Example:

A ProgressBar Control shows the progress of a file copy operation using the following example. The example uses the minimum and maximum properties to specify a range of file to be copied for the ProgressBar.


private void CopyWithProgress(string[] filenames)
{
	// Display the ProgressBar control.
	progressBar1.Visible = true;
	// Set Minimum to 1 to represent the first file being copied.
	progressBar1.Minimum = 1;
	// Set Maximum to the total number of files to copy.
	progressBar1.Maximum = filenames.Length;
	// Set the initial value of the ProgressBar.
	progressBar1.Value = 1;
	// Set the Step property to a value of 1 to represent each file being copied.
	progressBar1.Step = 1;
	
	// Loop through all files to copy.
	for (int x = 1; x <= filenames.Length; x++)
	{
		// Copy the file and increment the ProgressBar if successful.
		if(CopyFile(filenames[x-1]) == true)
		{
			// Perform the increment on the ProgressBar.
			progressBar1.PerformStep();
		}
	}
}

 

This example requires a ProgressBar control called progressBar1 created within a Form and a CopyFile method (which returns a boolean value that shows a file copy operation was successfully complete) that performs the file copy operation.

Complete Source Code:


using System;
using System.IO;
using System.Windows.Forms;

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

        private void btnCopy_Click(object sender, EventArgs e)
        {
            string[] file ={ @"C:\Users\rohit\Downloads\1.mp4", @"C:\Users\rohit\Downloads\2.zip", @"C:\Users\rohit\Downloads\3.zip" };
            CopyWithProgress(file);
            
        }
        private void CopyWithProgress(string[] filenames)
        {
            // Display the ProgressBar control.
            progressBar1.Visible = true;
            // Set Minimum to 1 to represent the first file being copied.
            progressBar1.Minimum = 1;
            // Set Maximum to the total number of files to copy.
            progressBar1.Maximum = filenames.Length;
            // Set the initial value of the ProgressBar.
            progressBar1.Value = 1;
            // Set the Step property to a value of 1 to represent each file being copied.
            progressBar1.Step = 1;

            // Loop through all files to copy.
            for (int x = 1; x <= filenames.Length; x++)
            {
                // Copy the file and increment the ProgressBar if successful.
                if (CopyFile(filenames[x - 1]) == true)
                {
                    // Perform the increment on the ProgressBar.
                    progressBar1.PerformStep();
                }
            }
        }

        private bool CopyFile(string v)
        {
            File.Copy(v,Path.GetFileName(v));
            return true;
        }
    }
}

 

Progress Bar Control C# GUI

Progress Bar Control

Video Tutorial