C# Program to Create a Progress Bar Control

This is a C# Program to create a progress bar control.

Problem Description

This C# Program Creates a Progress Bar Control.

Problem Solution

Here ProgressBar indicates visually the progress of an operation. It is best used on a long-running computation or task.

Program/Source Code

Here is source code of the C# Program to Create a Progress Bar Control. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Create a Progress Bar Control
 */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
namespace ProgressBar
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            for (int i = 1; i <= 100; i++)
            {
                System.Threading.Thread.Sleep(100);
                backgroundWorker1.ReportProgress(i);
            }
        }
 
        private void backgroundWorker1_ProgressChanged(object sender, 
         ProgressChangedEventArgs e)
        {
            progressBar1.Value = e.ProgressPercentage;
            this.Text = e.ProgressPercentage.ToString();
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            backgroundWorker1.RunWorkerAsync();
        }
    }
}
Program Explanation

This C# program is used to create a progress bar control. A Progress Bar control is used to represent the progress of a lengthy operation that takes time where a user must wait for the operation to be finished.

advertisement
advertisement

The Thread.Sleep() function is used to suspend the current thread for the specified number of milliseconds. In progress changed method assigns the value to Progress Bar using ‘ProgressPercentage’ which is used to get the task progress percentage.

Then RunWorkerAsync() function is used to start execution of a background operation. Here Progress Bar indicates visually the progress of an operation. It is best used on a long-running computation or task.

Runtime Test Cases

output

Note: Join free Sanfoundry classes at Telegram or Youtube

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

If you wish to look at all C# Programming examples, go to 1000 C# Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.