中的setupform中使用progressbarcontrol

中的setupform中使用progressbarcontrol

本文介绍了如何在C#中的setupform中使用progressbarcontrol?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请回答上述问题,并提供示例代码.

Please reply to the above question and give me the sample code.

推荐答案

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();
    }
}


这篇关于如何在C#中的setupform中使用progressbarcontrol?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 05:49