Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fix progress bar in WinForms

The problem is the label gets displayed before the progress-bar is completed. How do I make the label display only after the progress-bar is fully complete?

I tried changing the max values to a higher number but it didn't work.

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;
            }
        }
    }
}
like image 703
Adarsh Avatar asked Dec 19 '22 09:12

Adarsh


1 Answers

Actually, Your code run very vast, it is about less than a second to set value from 0% to 100% ! But ProgressBar has two styles for displaying the current status (Classic and Continues).

In Continues mode if the progress value went to 100% from 0% the control will show an animation, which is not display the real and accurate progress. You can set a delay via Thread.Sleep() and show your label immediately after the for loop to find out to what i happening !

The Below code will work:

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;
}
like image 199
Siavash Avatar answered Dec 24 '22 00:12

Siavash