【问题标题】:How do I show status in a progress bar on my winform?如何在我的 winform 进度条中显示状态?
【发布时间】:2013-02-08 08:42:20
【问题描述】:

在将数据从一个表传输到另一个表时,我想要一个进度条来显示我的表单上的状态。我尝试了一些方法,但它不起作用。

if (dataGridView1.RowCount - 1 == no_Of_rows)
{
    progressBar1.Value = 100;
    dataGridView1.Visible = true;
}
else
{
    progressBar1.Value = 50;
    dataGridView1.Visible = false;
}

【问题讨论】:

  • 显示代码并告诉 what 不起作用。
  • 使用 BackgroundWorker,查找类,从那里开始。
  • if (dataGridView1.RowCount - 1 == no_Of_rows) { progressBar1.Value = 100; dataGridView1.Visible = true; } else { 进度条1.Value = 50; dataGridView1.Visible = false; }
  • 请不要将代码发布为评论。更新您的帖子(或添加答案)并使用代码标签

标签: c# winforms c#-4.0 progress-bar


【解决方案1】:

【讨论】:

    【解决方案2】:

    这是一个如何使用后台工作程序的示例。

    private BackgroundWorker worker;
        public Form1()
        {
            InitializeComponent();
    
    
            this.worker = new BackgroundWorker
                {
                    WorkerReportsProgress = true
                };
            worker.DoWork += WorkerOnDoWork;
            worker.ProgressChanged += WorkerOnProgressChanged;
            worker.RunWorkerCompleted += delegate
                {
                    //Set the value of the progressbar to the maximum value if the work is done
                    this.progressBar1.Value = this.progressBar1.Maximum;
                };
            worker.RunWorkerAsync();
        }
    
        private void WorkerOnProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            //Set the value of the progressbar, or increment it.
            //You can use the e.ProgressPercentage to get the value you set in the DoWork-Method
            //The e.UserState ist a custom-value you can pass from the DoWork-Method to this Method
            this.progressBar1.Increment(1);
        }
    
    
        private void WorkerOnDoWork(object sender, DoWorkEventArgs e)
        {
            // Do you stuff here. Here you can tell the backgroundworker to report the progress like.
            this.worker.ReportProgress(5);
            //You can not access properties from here, so if you want to pass a value or something else to the
            //progresschanged-method you have to use e.Argument. 
    
        }
    

    这是一个 winforms 应用程序。在表单上只有一个名为 progressbar1 的进度条

    【讨论】:

      最近更新 更多