Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Text over Progressbar - Question

Text over Progressbar - Question

Scheduled Pinned Locked Moved C#
graphicsquestion
4 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Programm3r
    wrote on last edited by
    #1

    Hi all, I found the following code, that allows one to write a string on a ProgressBar ... Only thing is that I can't get it to work :confused::confused: Can anyone else make it work, and if so ... what am I doing wrong? // call the method SetProgressBarText(progressBar1, null, ProgressBarTextLocation.Centered, Color.Black, SystemFonts.DefaultFont); // Set the ProgressBar Text method private void SetProgressBarText(System.Windows.Forms.ProgressBar Target, //The target progress bar string Text, //The text to show in the progress bar ProgressBarTextLocation Location, //Where the text is to be placed System.Drawing.Color TextColor, //The color the text is to be drawn in System.Drawing.Font TextFont //The font we use to draw the text ) { //Make sure we didn't get a null progress bar if (Target == null) throw new ArgumentException("Null Target"); //Now we can get to the real code //Check to see if we are to add in the percent if (string.IsNullOrEmpty(Text)) { //We are to add in the percent meaning we got a null or empty Text //We give text a string value representing the percent int percent = (int)(((double)(Target.Value - Target.Minimum) / (double)(Target.Maximum - Target.Minimum)) * 100); Text = percent.ToString() + "%"; } //Now we can add in the text //gr will be the graphics object we use to draw on Target using (Graphics gr = Target.CreateGraphics()) { gr.DrawString(Text, TextFont, //The font we will draw it it (TextFont) new SolidBrush(TextColor), //The brush we will use to draw it //Where we will draw it new PointF( // X location (Center or Left) Location == ProgressBarTextLocation.Left ? 5 : //Left side progressBar1.Width / 2 - (gr.MeasureStri

    P F 2 Replies Last reply
    0
    • P Programm3r

      Hi all, I found the following code, that allows one to write a string on a ProgressBar ... Only thing is that I can't get it to work :confused::confused: Can anyone else make it work, and if so ... what am I doing wrong? // call the method SetProgressBarText(progressBar1, null, ProgressBarTextLocation.Centered, Color.Black, SystemFonts.DefaultFont); // Set the ProgressBar Text method private void SetProgressBarText(System.Windows.Forms.ProgressBar Target, //The target progress bar string Text, //The text to show in the progress bar ProgressBarTextLocation Location, //Where the text is to be placed System.Drawing.Color TextColor, //The color the text is to be drawn in System.Drawing.Font TextFont //The font we use to draw the text ) { //Make sure we didn't get a null progress bar if (Target == null) throw new ArgumentException("Null Target"); //Now we can get to the real code //Check to see if we are to add in the percent if (string.IsNullOrEmpty(Text)) { //We are to add in the percent meaning we got a null or empty Text //We give text a string value representing the percent int percent = (int)(((double)(Target.Value - Target.Minimum) / (double)(Target.Maximum - Target.Minimum)) * 100); Text = percent.ToString() + "%"; } //Now we can add in the text //gr will be the graphics object we use to draw on Target using (Graphics gr = Target.CreateGraphics()) { gr.DrawString(Text, TextFont, //The font we will draw it it (TextFont) new SolidBrush(TextColor), //The brush we will use to draw it //Where we will draw it new PointF( // X location (Center or Left) Location == ProgressBarTextLocation.Left ? 5 : //Left side progressBar1.Width / 2 - (gr.MeasureStri

      P Offline
      P Offline
      Programm3r
      wrote on last edited by
      #2

      Nevermind :doh: :sigh: .... just through the method call within a Timer Tick and everything works ....

      The only programmers that are better C# programmers, are those who look like this -> :bob:

      :java: Programm3r My Blog: ^_^

      1 Reply Last reply
      0
      • P Programm3r

        Hi all, I found the following code, that allows one to write a string on a ProgressBar ... Only thing is that I can't get it to work :confused::confused: Can anyone else make it work, and if so ... what am I doing wrong? // call the method SetProgressBarText(progressBar1, null, ProgressBarTextLocation.Centered, Color.Black, SystemFonts.DefaultFont); // Set the ProgressBar Text method private void SetProgressBarText(System.Windows.Forms.ProgressBar Target, //The target progress bar string Text, //The text to show in the progress bar ProgressBarTextLocation Location, //Where the text is to be placed System.Drawing.Color TextColor, //The color the text is to be drawn in System.Drawing.Font TextFont //The font we use to draw the text ) { //Make sure we didn't get a null progress bar if (Target == null) throw new ArgumentException("Null Target"); //Now we can get to the real code //Check to see if we are to add in the percent if (string.IsNullOrEmpty(Text)) { //We are to add in the percent meaning we got a null or empty Text //We give text a string value representing the percent int percent = (int)(((double)(Target.Value - Target.Minimum) / (double)(Target.Maximum - Target.Minimum)) * 100); Text = percent.ToString() + "%"; } //Now we can add in the text //gr will be the graphics object we use to draw on Target using (Graphics gr = Target.CreateGraphics()) { gr.DrawString(Text, TextFont, //The font we will draw it it (TextFont) new SolidBrush(TextColor), //The brush we will use to draw it //Where we will draw it new PointF( // X location (Center or Left) Location == ProgressBarTextLocation.Left ? 5 : //Left side progressBar1.Width / 2 - (gr.MeasureStri

        F Offline
        F Offline
        firda cze
        wrote on last edited by
        #3

        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, 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; }
        }
        }

        S 1 Reply Last reply
        0
        • F firda cze

          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, 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; }
          }
          }

          S Offline
          S Offline
          Scott Sherin
          wrote on last edited by
          #4

          Thanks firda, been looking for that code all over.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups