How to change the color and the font of the StatusBarPanel object by using Visual C#

Article translations Article translations
Article ID: 319311 - View products that this article applies to.
This article was previously published under Q319311
Expand all | Collapse all

On This Page

SUMMARY

This step-by-step article demonstrates how to programmatically set the color and the font of the StatusBarPanel object by using Visual C#.

The StatusBar control includes a Panels property, which is a collection of StatusBarPanel objects. The StatusBarPanel class does not have any members that allow you to change of the background color or the font. However, you can use GDI+ to paint the panel with a background color and to draw the text by using a font and a color that you specify.

Steps to Create the Sample

  1. Follow these steps to create a Windows Application in Visual C# :
    1. Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
    2. On the File menu, point to New, and then click Project.
    3. In the New Project dialog box, click Visual C# Projects under Project Types, and then click Windows Application under Templates. By default, Form1 is created.

      Note In Visual Studio 2005, click Visual C# under Project Types.
  2. Add a StatusBar control to Form1. By default, the control is named StatusBar1.
  3. In the Properties window of StatusBar1, click Panels, and then click the ellipsis button next to the Panels property.
  4. Follow these steps in the StatusBarPanel Collection Editor dialog box:
    1. Click Add three times to add three panels to the StatusBar control. By default, the panels are named StatusBarPanel1, StatusBarPanel2, and StatusBarPanel3.
    2. Change the Style property of each panel to OwnerDraw.
    3. Click OK to close the StatusBarPanel Collection Editor dialog box.
  5. In the Properties window of StatusBar1, change the ShowPanels property to True.
  6. Double-click StatusBar1 to open the code window of Form1, and then add the following code to declare variables in the Form1 class:
    Pen p = new Pen(Color.Yellow);
    SolidBrush brYellowFontBrush = new SolidBrush(Color.Yellow);
    SolidBrush[] arBrushes = new SolidBrush[3];
    					
  7. Add the following code in the InitializeComponent method:
    arBrushes[0] = new SolidBrush (Color.Blue);
    arBrushes[1] = new SolidBrush (Color.Green);
    arBrushes[2]= new SolidBrush (Color.Pink);
    this.statusBar1.DrawItem += new System.Windows.Forms.StatusBarDrawItemEventHandler(this.statusBar1_DrawItem); 
    					
  8. Replace the code in the Dispose method with the following code:
    protected override void Dispose( bool disposing )
    {
    	if( disposing )
    	{
    		if (components != null) 
    		{
    			components.Dispose();
    		}
    	}
    	p.Dispose();
    	brYellowFontBrush.Dispose();
    	int i;
    	for (i = 0; i < arBrushes.Length; i++)
    		arBrushes[i].Dispose();
    	base.Dispose( disposing );
    }
    					
  9. Add the following code in Form1 class:
    private void statusBar1_DrawItem(object sender, System.Windows.Forms.StatusBarDrawItemEventArgs sbdevent)
    {		
            Graphics g = sbdevent.Graphics;
    	StatusBar sb = (StatusBar)sender;
    	RectangleF rectf = new RectangleF(sbdevent.Bounds.X, sbdevent.Bounds.Y, sbdevent.Bounds.Width, sbdevent.Bounds.Height);
    			
    	g.DrawRectangle(p, sbdevent.Bounds);
    	sbdevent.Graphics.FillRectangle(arBrushes[sbdevent.Index], sbdevent.Bounds);
    	g.DrawString("Panel1", sb.Font, brYellowFontBrush, rectf);
    } 
    					
  10. Press F5 to run the application.

Properties

Article ID: 319311 - Last Review: December 11, 2006 - Revision: 4.4
APPLIES TO
  • Microsoft Visual C# .NET 2003 Standard Edition
  • Microsoft Visual C# .NET 2002 Standard Edition
  • Microsoft .NET Framework 1.1
  • Microsoft .NET Framework 1.0
  • Microsoft Visual C# 2005 Express Edition
Keywords: 
kbctrl kbhowtomaster KB319311

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com