Show Text on ProgressBar in StatusStrip

6.2k Views Asked by At

I am new in windows programming. In C# I was working with using Visual Studio 2017. Now, Im stuck with a problem. The Problem is that, I am trying to display some text (the progress value) in ProgressBar in the StatusStrip but can't find a proper working way to do that. :-(

Can anyone please provide me some ideas or solution to this problem? I shall be glad and thankful to you ! Your answers will be appreciated greatly. :-)

3

There are 3 best solutions below

11
Reza Aghaei On BEST ANSWER

ToolStripProgressBar component uses a ProgressBar to show the progress and the control doesn't show text. To be able to render a text for control, you need to paint the value yourself. To do so, you can create a custom item deriving from ToolStripProgressBar. Then you can use NativeWindow to assign a custom code to WM_PAINT message of the control:

enter image description here

using System;
using System.Windows.Forms;
using System.Windows.Forms.Design;
[ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.All)]
public class MyToolStripProgressBar : ToolStripProgressBar
{
    public MyToolStripProgressBar() : base()
    {
        this.Control.HandleCreated += Control_HandleCreated;
    }
    private void Control_HandleCreated(object sender, EventArgs e)
    {
        var s = new ProgressBarHelper((ProgressBar)this.Control);
    }
}
public class ProgressBarHelper : NativeWindow
{
    ProgressBar c;
    public ProgressBarHelper(ProgressBar progressBar)
    {
        c = progressBar;
        this.AssignHandle(c.Handle);
    }
    protected override void WndProc(ref Message m)
    {
        base.WndProc(ref m);
        if (m.Msg == 0xF /*WM_PAINT*/)
        {
            using (var g = c.CreateGraphics())
                TextRenderer.DrawText(g, c.Value.ToString(),
                   c.Font, c.ClientRectangle, c.ForeColor);
        }
    }
}
3
Pio On
  progressBar1.CreateGraphics().DrawString(progressBar1.Value.ToString(), new Font("Arial", (float)8.25, FontStyle.Regular), Brushes.Black, new PointF(progressBar1.Width / 2 - 10, progressBar1.Height / 2 - 7));

I had the same question just a jew days ago an ran into this little piece of code that does the job perfectly :)
Got the code and explanation from this Website

0
user22540528 On

In Microsoft Visual Studio 2022 (and perhaps some earlier versions)

  • Click on the status bar in the form designer.
  • Click the small drop down arrow to the right of the status bar elements.
  • Select the Progress bar item.

A class of type System.Windows.Forms.ToolStripProgressBar is created with a name like toolStripProgressBar1.

Items can be deleted from the status bar by selecting them and clicking delete.