Click here to Skip to main content
15,920,438 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Please reply to the above question and give me the sample code.
Posted
Updated 20-Mar-10 4:45am
v2

1 solution

Drag a ProgressBar onto the Form. When you need to set a new progress percentage, set the ProgressBar's Value property.

You may find it better to use a BackgroundWorker to do the 'set up' in it's DoWork event handler. Set it's WorkerReportsProgress property to true and handle it's ProgressChanged event.

When you need to update the progress, call the BackgroundWorker's ReportProgress method and in the handler update the ProgressBar
C#
using System;
using System.ComponentModel;
using System.Threading;
using System.Windows.Forms;
public partial class FormSetUp : Form
{
    BackgroundWorker backgroundWorker;
    ProgressBar progressBar;
    public FormSetUp()
    {
        InitializeComponent();
        Shown += new EventHandler(FormSetUp_Shown);
        backgroundWorker = new BackgroundWorker();
        backgroundWorker.WorkerReportsProgress = true;
        backgroundWorker.DoWork += new DoWorkEventHandler(
            backgroundWorker_DoWork);
        backgroundWorker.ProgressChanged += new ProgressChangedEventHandler(
            backgroundWorker_ProgressChanged);
        backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(
            backgroundWorker_RunWorkerCompleted);
        progressBar = new ProgressBar();
        Controls.Add(progressBar);
    }
    void FormSetUp_Shown(object sender, EventArgs e)
    {
        backgroundWorker.RunWorkerAsync();
    }
    void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
    {
        int percentage = 0;
        backgroundWorker.ReportProgress(percentage);
        // Do something
        Thread.Sleep(1000); // Simulate work
        percentage += 50;
        backgroundWorker.ReportProgress(percentage);
        // Do something else
        Thread.Sleep(1000); // Simulate work
        percentage += 50;
        backgroundWorker.ReportProgress(percentage);
        Thread.Sleep(1000);
    }
    void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        progressBar.Value = e.ProgressPercentage;
    }
    void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        Close();
    }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900