修复WinForms中的进度条

时间:2017-02-08 14:37:45

标签: c# winforms progress-bar

问题是标签在progress-bar完成之前显示。如何在label完全完成后才能显示progress-bar

我尝试将最大值更改为更高的数字但不起作用。

public partial class dload : Form
{
    public dload()
    {
        InitializeComponent();
    }

    private void dload_Load(object sender, EventArgs e)
    {
        label1.Visible = false;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        int i = 0;
        progressBar1.Minimum = 0;
        progressBar1.Maximum = 5000;
        for (i = 0; i <= 5000; i++)
        {
            progressBar1.Value = i;
            if (i == 5000)
            {
                label1.Visible = true;
            }
        }
    }
}

3 个答案:

答案 0 :(得分:6)

实际上,您的代码运行非常庞大,将值从0%设置为100%不到一秒钟!但 ProgressBar有两种显示当前状态的样式(ClassicContinues)。

Continues模式下,如果进度值从0%升至100%,则控件将显示动画,但不会显示真实准确的进度。您可以通过Thread.Sleep()设置延迟,并在for循环后立即显示标签,以了解我的情况!

以下代码将起作用:

private void button1_Click(object sender, EventArgs e)
{
    int i = 0;
    progressBar1.Minimum = 0;
    progressBar1.Maximum = 5000;
    for (i = 0; i <= 5000; i++)
    {
        Thread.Sleep(1);
        progressBar1.Value = i;

    }
    label1.Visible = true;
}

答案 1 :(得分:3)

这是一个动画问题。 A&#34; hack&#34;它实际上是将进度值减少1:

progressBar1.Minimum = 0;
progressBar1.Maximum = 5000;
for (int i = 0; i < progressBar1.Maximum; i++) {
  progressBar1.Value = i;
  progressBar1.Value = Math.Max(i - 1, progressBar1.Minimum);
}
label1.Visible = true;

答案 2 :(得分:1)

使用progressBar1.Refresh

public partial class dload : Form
{
    public dload()
    {
        InitializeComponent();
    }

private void dload_Load(object sender, EventArgs e)
{
    label1.Visible = false;
}

private void button1_Click(object sender, EventArgs e)
{
    int i = 0;
    progressBar1.Minimum = 0;
    progressBar1.Maximum = 5000;
    for (i = 0; i <= 5000; i++)
    {
        progressBar1.Value = i;
        progressBar1.Refresh();
        if (i == 5000)
        {
            label1.Visible = true;
        }
    }
}