Incrementing Progressbar with a Timer causes a Visual Bug! The Progress only carries half but the value is FULL! c#

Asked

Viewed 57 times

0

Hi, I’m creating a project something like this: Clicko and a Proressbar appears, I increment with a Timer, I counted and qd an incremented variable of 1 in 1 reaches 11, we have +/- 3 seconds, is what I want! Then the progressibar needs to be "reset" and disappear. The process suddenly appears whenever I "clicko". BUT, Qd I do this with the progression and the timer, of a visual bug: the progressibar does not load up to the maximum, it carries about 60%, 40%, and says that the value is already full(11)! I need help, it’s for a school project!

Obs.: This code and the print are not from the project, only an example I did to exemplify the problem.

Code(Did not fit very well, see everything, please):

private void button1_Click_1(object sender, EventArgs e)
        {
            progressBar1.Enabled = true;
            progressBar1.Visible = true;
            timer1.Start();

        }

        private void timer1_Tick_1(object sender, EventArgs e)
        {

            //progressBar1.Increment(1);
            progressBar1.Value += 1;
            if (progressBar1.Value == 11) // 11 é o valor Maximo da ProgressBar
            {
                timer1.Stop();
                progressBar1.Value = 0;
                MessageBox.Show("oi");
                progressBar1.Enabled = false;
                progressBar1.Visible = true;


            }
        }
  • 1

    Sometimes it is necessary to force the visual upgrade of the components. Use progressBar1.Refresh() or progressBar1.Invalidate() after progressBar1.Value += 1;

  • It did not work not ;--; I used the 2 or only one of the 2, in various parts but it did not work at all :(

  • How long is the timer set?

  • the same pattern, 100.

1 answer

0


According to this response from ONLY. This is a known problem of Windows.

The C# uses the component ProgressBar window native. Starting with Windows 7, more "soft" animations were made on the components, causing a small delay in their animations.

But there is a small detail, when you decrease the value of ProgressBar there is no such delay. This is because this smooth animation was not implemented by decreasing the value of ProgressBar.

Soon we can take advantage of this situation to force the quick update of ProgressBar. The trick is to increase the value and decrease and increase again.

Example:

progressBar1.Value += 1;
progressBar1.Value -= 1;
progressBar1.Value += 1;

And when you get to the top, do it like this:

progressBar1.Maximum += 1;
progressBar1.Value += 1;
progressBar1.Value -= 1;
progressBar1.Maximum -= 1;

I myself had found this solution elsewhere already, but had not understood why it worked. I even looked up how the component worked ProgressBar, but basically the c# only interacts through messages with it.

It is not the best solution of all, until it seems a "gambiarra", however it works...

  • It worked! Still a little more only when it arrives at the end, like, it doesn’t carry the last one, but anyway it’s worth a lot! Serio, mt obg xD

  • Ball show, it’s a bit buggy even. If it helped you and worked can mark this answer as correct.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.