c# - 进度条值只是闪烁并消失

标签 c# winforms

我试图将百分比值放入我的进度条中,但它看起来像闪烁然后消失了。

这是我的 ProgressChanged 事件:

C#代码

public void button1_click(object sender, EventArgs e)
{
BackgroundWorker _worker = new BackgroundWorker();
                _worker.WorkerReportsProgress = true;
                _worker.ProgressChanged += new ProgressChangedEventHandler(_worker_ProgressChanged);
                _worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(_worker_RunWorkerCompleted);                
                _worker.DoWork += (s, e2) =>
                {
                    for (int i = 0; i <= xmlnode.Count - 1; i++)
                    {
                         _worker.ReportProgress((int)100 * i / (xmlnode.Count - 1));
                         // Many validations here
                    }
                 }
}

void _worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {            
            progressBar1.Value = e.ProgressPercentage;            

             // Here I tryied to put the percentage value in progress bar

            int percent = (int)(((double)progressBar1.Value / (double)progressBar1.Maximum) * 100);
            progressBar1.CreateGraphics().DrawString(percent.ToString() + "%", new Font("Arial", (float)8.25, FontStyle.Regular), Brushes.Black, new PointF(progressBar1.Width / 2 - 10, progressBar1.Height / 2 - 7));
        }

最佳答案

文本只是闪烁,因为它没有在 Paint 上重新绘制。使用进度条的 Paint 事件并在那里绘制百分比。您只需将其存储在类变量中即可轻松获取该百分比。

关于c# - 进度条值只是闪烁并消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17525531/

相关文章:

c# - 用渐变颜色绘制一条线

c# - 我怎样才能制作一个 pictureBox,当点击它时,它会在标签上显示一些文本?

c# - 如何在 C# 中以编程方式自动启动 winform 应用程序?

c# - 将 2 个整数(整数和小数部分)连接到 C# 中的 double

c# - 哪个执行速度更快或更优化? C#

C# 约定/最佳实践

c# - 刷新按钮 - 在插入、删除、更新后刷新数据 GridView

c# - 从指定时间开始秒表

c# - DataGridView.CellContentClick

c# - 多个Sql插入字符串并使用Catch回滚(异常)