0

I need a bar which indicate a percentage of something, like battery engry or used space size of a hard disk. Likeenter image description here

This is not a progress bar. When I use progress bar, each time I send the message PBM_SETPOS, it will increase from 0 to the pos.

The bar must have the ability for me to set a value manually, and after it, it will change the color region to the specified length directly, rather than in a increasement way.

Does WinAPI has a build in function for this purpose?

user565739
  • 1,302
  • 4
  • 23
  • 46
  • 3
    You can use a progress bar for this, you just need to workaround one of the *enhancements* added in Vista. Namely the animation used when you increase position. Read my answer here to learn how: http://stackoverflow.com/questions/6128287/tprogressbar-never-fills-up-all-the-way-seems-to-be-updating-too-fast/6128562#6128562 – David Heffernan May 23 '12 at 08:26
  • 1
    So `Every time you set the progress bar counter to N immediately afterwards set it to N-1.` is the solution? I will try it, but sound weired.....Thank you – user565739 May 23 '12 at 08:32
  • 2
    That's right. It's weird but that's how it rolls with that control. – David Heffernan May 23 '12 at 08:36
  • Thank you again, David. It works!! Just curious that hwo could you know this "very weired" method? Is it documented somewhere? By the way, do you know a way to get the blue color as above picture? Since progress bar only has green, yellow, red color. Or the bar for showing the used disk space is not made by progress bar, so it has more color to use? I know there is a way that needs disable Visual style, but as the above picture, it use Visual style, I think – user565739 May 23 '12 at 08:55
  • I don't think it's documented anywhere. I learnt about it whilst trying to answer a Stack Overflow question. I think I found it on a forum after doing a websearch. – David Heffernan May 23 '12 at 08:56
  • Possible duplicate of [How can I use a meter-style progress bar?](http://stackoverflow.com/questions/2671366/how-can-i-use-a-meter-style-progress-bar) – jrh Aug 03 '16 at 19:03

1 Answers1

0

Use a Panel and then set a label with in it...Then set the width of label with backcolor based on percentage...

Code as follows:

string s27 = "select percentage from CandidateReg3 where candidateid='" + candidateid + "'";
DataTable d17 = cn.viewdatatable(s27);
if (d17.Rows.Count > 0) {
   for (int m = 0; m < d17.Rows.Count; m++) {
        percreg3 = int.Parse(d17.Rows[m]["percentage"].ToString());
        percregtotal = percreg3 + percreg;
   }
} else {
   percregtotal = percreg;
}

decimal percregtotals = (decimal)percregtotal / 100;
double percent = Convert.ToDouble(percregtotals);

// double percent = 1;
IndicatorLabel.Width = new Unit(percent * IndicatorPanel.Width.Value, UnitType.Pixel);

IndicatorLabel.ToolTip = percent.ToString("p0");
IndicatorLabel.Text = percent.ToString("p0"); 
IndicatorLabel.ForeColor=Color.White;

// IndicatorLabel.Text = "";
IndicatorLabel.BackColor = Color.Green;
Stephan
  • 41,764
  • 65
  • 238
  • 329
xyz
  • 1