如何在C#中的setupform中使用progressbarcontrol? [英] How to use progressbarcontrol in setupform in c#?

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

问题描述

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

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

推荐答案

ProgressBar拖到Form上.当您需要设置新的进度百分比时,请设置ProgressBarValue属性.

您可能会发现,使用BackgroundWorker在其DoWork事件处理程序中进行设置"会更好.将其WorkerReportsProgress属性设置为true并处理其ProgressChanged事件.

当您需要更新进度时,请调用BackgroundWorkerReportProgress方法,并在处理程序中更新ProgressBar
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
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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆