• (2 Pages)
  • +
  • 1
  • 2

Add the percent into a progress bar

#16 Bacanze  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 202
  • Joined: 09-April 10

Posted 15 April 2010 - 11:17 AM

Very useful, thanks.
Was This Post Helpful? 0
  • +
  • -

#17 Guest_igal*


Reputation:

Posted 18 April 2010 - 03:46 AM

View Postjacobjordan, on 12 October 2008 - 04:05 PM, said:

View Postgbertoli3, on 7 Oct, 2008 - 05:46 PM, said:

Every time it updates it hides the text then shows after it has updated. I tried using the Refresh() but it does nothing.

My code is the exact same as yours.

Also, I am putting this code in a timer, it will update every second.

Why use a timer? You only need to use this code after a line when you update the progress bar's value.


I have the same problem, but i'm not using timer, mu progress bar is in backgroundworker, and in "backgroundWorker_ProgressChanged" i'm changing the progress bar value, i tried to change the percents at the same time, but what i get it's only a blinking text...
any solution ?

tanks
Was This Post Helpful? 0

#18 Guest_firda*


Reputation:

Posted 19 April 2010 - 01:11 AM

public class ProgressLabel: ProgressBar {
	private static StringFormat sfCenter = new StringFormat() {
		Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center };
	private Color textColor = DefaultTextColor;
	private string progressString;
	public ProgressLabel() { SetStyle(ControlStyles.AllPaintingInWmPaint|ControlStyles.OptimizedDoubleBuffer, true); }
	protected override void OnCreateControl() {
		progressString = null;
		base.OnCreateControl();
	}
	protected override void WndProc(ref Message m) {
		switch(m.Msg) {
		case 15: if(HideBar) base.WndProc(ref m);
			else {
				ProgressBarStyle style = Style;
				if(progressString == null) {
					progressString = Text;
					if(!HideBar && style != ProgressBarStyle.Marquee) {
						int range = Maximum-Minimum;
						int value = Value;
						if(range > 42949672) { value = (int)((uint)value>>7); range = (int)((uint)range>>7); }
						if(range > 0) progressString = string.Format(progressString.Length == 0 ? "{0}%" : "{1}: {0}%",
						value*100/range, progressString);
					}
				}
				if(progressString.Length == 0) base.WndProc(ref m);
				else using(Graphics g = CreateGraphics()) {
					base.WndProc(ref m);
					OnPaint(new PaintEventArgs(g, ClientRectangle));
				}
			}
			break;
		case 0x402: goto case 0x406;
		case 0x406: progressString = null;
			base.WndProc(ref m);
			break;
		default:
			base.WndProc(ref m);
			break;
		}
	}
	protected override void OnPaint(PaintEventArgs e) {
		Rectangle cr = ClientRectangle;
		RectangleF crF = new RectangleF(cr.Left, cr.Top, cr.Width, cr.Height);
		using(Brush br = new SolidBrush(TextColor))
			e.Graphics.DrawString(progressString, Font, br, crF, sfCenter);
		base.OnPaint(e);
	}
	public bool HideBar {
		get { return GetStyle(ControlStyles.UserPaint); }
		set { if(HideBar != value) { SetStyle(ControlStyles.UserPaint, value); Refresh(); } }
	}
	public static Color DefaultTextColor {
		get { return SystemColors.ControlText; }
	}
	public Color TextColor {
		get { return textColor; }
		set { textColor = value; }
	}
	public override string Text {
		get { return base.Text; }
		set { if(value != Text) { base.Text = value; progressString = null; } }
	}
	public override Font Font {
		get { return base.Font; }
		set { base.Font = value; }
	}
}


Was This Post Helpful? 0

#19 typhooncoder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 18-February 14

Posted 03 March 2014 - 01:18 PM

HI, My disappears after the progress bar updates, why is this?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2