How to change the color and the font of the StatusBarPanel object by using Visual C# (319311)
The information in this article applies to:
- Microsoft Visual C# .NET (2003)
- Microsoft Visual C# .NET (2002)
- 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 TASKSUMMARY
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.
- 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.
- Change the Style property of each panel to OwnerDraw.
- Click OK to close the StatusBarPanel Collection Editor dialog box.
- In the Properties window of StatusBar1, change the ShowPanels property to True.
- 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];
- 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);
- 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 );
}
- 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);
}
- Press F5 to run the application.
back to the top
Modification Type: | Major | Last Reviewed: | 1/18/2006 |
---|
Keywords: | kbCtrl kbHOWTOmaster KB319311 kbAudDeveloper |
---|
|