Microsoft KB Archive/319312

= HOW TO: Change the Color and the Font of the StatusBarPanel Object by Using Visual Basic .NET =

Article ID: 319312

Article Last Modified on 3/24/2004

-

APPLIES TO


 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft .NET Framework 1.1
 * Microsoft .NET Framework 1.0
 * Microsoft .NET Framework Class Libraries 1.1
 * Microsoft .NET Framework Class Libraries 1.0

-



This article was previously published under Q319312



For a Microsoft Visual C# .NET version of this article, see 319311.

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 Basic .NET.

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 Basic .NET:  Start Microsoft Visual Studio .NET. On the File menu, point to New, and then click Project. In the New Project dialog box, click Visual Basic Projects under Project Types, and then click Windows Application under Templates. By default, Form1 is created.  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 in the Form1 class: Dim p As Pen = New Pen(Color.White) Dim brYellowFontBrush = New SolidBrush(Color.Yellow) Dim arBrushes(2) As SolidBrush

Private Sub StatusBar1_DrawItem(ByVal sender As Object, _ ByVal sbdevent As System.Windows.Forms.StatusBarDrawItemEventArgs) _ Handles StatusBar1.DrawItem Dim g As Graphics = sbdevent.Graphics Dim sb As StatusBar = CType(sender, StatusBar) Dim 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;Panel&quot; & sbdevent.Index, sb.Font, brYellowFontBrush, rectf) End Sub

Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load Dim i As Integer

arBrushes(0) = New SolidBrush(Color.Blue) arBrushes(1) = New SolidBrush(Color.Green) arBrushes(2) = New SolidBrush(Color.Pink) End Sub </li>  Replace the code in the Dispose method of Form1 with the following code: Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose End If

Dim i As Integer p.Dispose brYellowFontBrush.Dispose For i = 0 To arBrushes.Length - 1 arBrushes(i).Dispose Next End If    MyBase.Dispose(disposing) End Sub </li> Press F5 to run the application.</li></ol>

back to the top

Keywords: kbhowtomaster KB319312

-

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

© Microsoft Corporation. All rights reserved.