C# – How to use ProgressBar control?

ProgressBar control we use to display the status of the lengthy operation. When we download a file or copy a file to another location; usually we show the status of the operation through a progress bar, and the Application interface looks more clean.

ProgressBar class in .Net represents the ProgressBar control. Through this article we are going to discuss the usage of ProgressBar control in C# Windows Forms Application.

Step 1. Add ProgressBar control to the Windows Form in our C# Windows Forms Application.

Step 2. We need to set the progress bar progress boundaries; that means, the minimum and maximum values of the control to show the progress of the operation or task.

For example, if we set 0 and 100 as the progress boundaries; when the value of the control changed from 0 to 100, the progress shows on the control. Progress completion means, the task is 100% completed. 

ProgressBar class has Minimum and Maximum properties to set the minimum and maximum values for the control. These indicates the progress boundaries of the control.

Step 3. How does the ProgressBar control know the progress of the task? It doesn’t know. We need to specify that through it’s Value property. Value indicates the current status of the progress; if the Value is reached the Maximum value; it indicates the task is completed. By setting this Value property, the control shows the progress.

We can set the progress in other ways too. By calling PerformStep() method; or by calling Increment() method; we can alter the progress status of the control.

A Step property has to be set for PerformStep() method, before we alter the progress. For example, if we set the value for Step property as 1; whenever we call PerformStep() method, it increments the value of Value property by 1; and which moves the progress.

Another interesting thing with Step property is it allows negative (-ve) values. Yes, that’s correct. This is useful, when you want to cancel the operation. In the progress bar, it shows the progress in decreasing order; that shows to the user that the operation is cancelling.

When calling Increment() method, we need to pass how much the progress has to increment, by specifying it through it’s argument. For example, Increment(10) will increment the current progress value by 10.

This continues, until the operation is completed.

Putting it altogether, here is the complete working code.

using System;
using System.Windows.Forms;

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

        private void BtnStart_Click(object sender, EventArgs e)
        {
            progressBar1.Value = 0;
            progressBar1.Minimum = 0;
            progressBar1.Maximum = 100;
            progressBar1.Step = 1;

            for (int i = 0; i < 100; i++)
            {
                progressBar1.PerformStep();
            }
        }
    }
}

Step 4. After build and run the above code, the Application window appears and it looks like below;

ProgressBar control - Demo
ProgressBar control – Demo

(Raju)

Add a Comment

Your email address will not be published.