How to change the font colour of StatusBarPanel

Hi all,

May I know how to change the font colour of the StatusBarPanel

Please let me know. Thanks



Answer this question

How to change the font colour of StatusBarPanel

  • Rajesh77

    With the default StatusBarPanel you can't set the ForeColor. You can inhire the StatusBarPanel class and set the StatusBarPanel.Style property to StatusBarPanelStyle.OwnerDraw to draw your own colored text in the StatusBar.DrawItem event.

    Here is a little example:

    Interface and inhired class

    public interface IOwnerDrawPanel
    {
    void Paint( Graphics g, Rectangle bounds );
    }

    public class StatusBarPanelEx : StatusBarPanel, IOwnerDrawPanel
    {
    private Color _foreColor;

    public Color ForeColor
    {
    get
    {
    return _foreColor;
    }
    set
    {
    if( !_foreColor.Equals( value ) )
    {
    _foreColor = value;

    if( Parent != null )
    {
    Parent.Invalidate();
    }
    }
    }
    }

    public StatusBarPanelEx()
    {
    _foreColor = Color.Black;
    Style = StatusBarPanelStyle.OwnerDraw;
    }

    public void Paint( Graphics g, Rectangle bounds )
    {
    if( Parent != null )
    {
    // Draw a blue background in the owner-drawn panel.
    g.FillRectangle(Brushes.AliceBlue, e.Bounds);

    // Create a StringFormat object to align text in
    // the panel and create the fore color brush.
    using( StringFormat textFormat = new StringFormat() )
    using( Brush brush = new SolidBrush( ForeColor ))
    {
    textFormat.LineAlignment = StringAlignment.Center;
    textFormat.Alignment = StringAlignment.Center;

    // Draw the panel's text in dark blue using the Panel
    // and Bounds properties of the StatusBarEventArgs object
    // and the StringFormat object.
    g.DrawString( Text, Parent.Font, brush, bounds, textFormat);
    }
    }
    }
    }



    DrawItem event code

    private void statusBar1_DrawItem(object sender, StatusBarDrawItemEventArgs sbdevent)
    {
    IOwnerDrawPanel ownerDrawPanel = sbdevent.Panel as IOwnerDrawPanel;

    if( ownerDrawPanel != null )
    {
    ownerDrawPanel.Paint( sbdevent.Graphics, sbdevent.Bounds );
    }
    }




  • How to change the font colour of StatusBarPanel