C # Progressbar not updating accurately in Vista or Windows7

public partial class Form1 : Form
{
  //....
  private void timer1_Tick(object sender, EventArgs e)
  {
     if (this.progressBar1.Value >= 100)
     {
         this.timer1.Stop();
         this.timer1.Enabled = false;
     }
     else
     {
         this.progressBar1.Value += 10;
         this.label1.Text = Convert.ToString(this.progressBar1.Value);                
     }
  }
  //......
}

Here I used a timer to update the progress bar value. It works great in XP. But in Windows7 or Vista, when the progress value is set to 100, but the graphic progress is not 100!

A search in some threads found that its animation delayed in Vista / Windows7.

How to get rid of this thing?

I do not want to lose the look of Vista / Window7 using:

SetWindowTheme(progressBar1.Handle, " ", " ");
+3
source share
4 answers
public partial class Form1 : Form
{
  //....
  private void timer1_Tick(object sender, EventArgs e)
  {
    if (this.progressBar1.Value >= 100)
    {
     this.timer1.Stop();
     this.timer1.Enabled = false;
    }
    else
    {
      int tempValue = this.progressBar1.Value + 10;
      if (tempValue < 100 && tempValue >=0 )
      {
       this.progressBar1.Value = tempValue + 1;
       this.progressBar1.Value = tempValue;
      }
      else if (tempValue >= 100)
      {
       this.progressBar1.Value = 100;
       this.progressBar1.Value = 99;
       this.progressBar1.Value = 100;
      }
     this.label1.Text = Convert.ToString(this.progressBar1.Value);                
    }
  }

//......
}

Otherwise, the progress bar looks normal. But there had to be a standard way for progress bars. Idea taken from Fozi comment here

0
source

. . , (100%). 100%, . .

if (NewValue < progressBar.Maximum)
{
  progressBar.Value = NewValue + 1;
  progressBar.Value--;
}
else
{
  progressBar.Maximum++;
  progressBar.Value = progressBar.Maximum;
  progressBar.Value--;
  progressBar.Maximum--;
}
+2

Here's how dumb progress indicators work in Vista and later.

No fix.

Complain to Microsoft.

+1
source
  private void timer1_Tick(object sender, EventArgs e)
    {

        if (progressBar1.Maximum == 1) progressBar1.Maximum = 100;
        if (progressBar1.Value==100) {
            progressBar1.Value = 0;
            return;
        }
        progressBar1.Increment(1);
        if (progressBar1.Value == 100) {
            progressBar1.Value = 1; progressBar1.Maximum = 1;
            progressBar1.Update();
        }
    }

These are my tricks to solve win7 problem with the right paint of the full progress bar.

+1
source

Source: https://habr.com/ru/post/1739948/


All Articles