CodeSteps

Python, C, C++, C#, PowerShell, Android, Visual C++, Java ...

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 show the status of the operation through a progress bar, and the Application interface looks cleaner.

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

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

Important properties of ProgressBar control

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 indicate 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 canceling.

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

This continues until the operation is completed.

Complete Working Code

Putting it all together, 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 building and running the above code, the Application window appears and it looks like below;

ProgressBar control - Demo
ProgressBar control – Demo

(Raju)

C# – How to use ProgressBar control?

One thought on “C# – How to use ProgressBar control?

  1. Wow, awesome blog layout! How long have you been blogging for?
    you make blogging glance easy. The entire look of
    your website is fantastic, let alone the content![X-N-E-W-L-I-N-S-P-I-N-X]I simply couldn’t leave
    your web site before suggesting that I really enjoyed the usual information a person supply for your visitors?
    Is gonna be again frequently in order to check out new
    posts.

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top