Microsoft KB Archive/319311

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

Article ID: 319311

Article Last Modified on 12/11/2006

-

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

-



This article was previously published under Q319311



For a Microsoft Visual Basic .NET version of this article, see 319312.

IN THIS TASK
SUMMARY
 * Steps to Create the Sample



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.

back to the top

Steps to Create the Sample
 Follow these steps to create a Windows Application in Visual C# :  Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005. On the File menu, point to New, and then click Project. 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.  Add a StatusBar control to Form1. By default, the control is named StatusBar1. In the Properties window of StatusBar1, click Panels, and then click the ellipsis button next to the Panels property.</li> Follow these steps in the StatusBarPanel Collection Editor dialog box:  Click Add three times to add three panels to the StatusBar control. By default, the panels are named StatusBarPanel1, StatusBarPanel2, and StatusBarPanel3.</li> Change the Style property of each panel to OwnerDraw.</li> Click OK to close the StatusBarPanel Collection Editor dialog box.</li></ol> </li> In the Properties window of StatusBar1, change the ShowPanels property to True.</li>  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]; </li>  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); </li>  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 ); }                   </li>  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(&quot;Panel1&quot;, sb.Font, brYellowFontBrush, rectf); }                    </li> Press F5 to run the application.</li></ol>

back to the top

Keywords: kbctrl kbhowtomaster KB319311

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.