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



The information in this article applies to:

  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual Basic .NET (2002)
  • 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

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

  1. Follow these steps to create a Windows Application in Visual Basic .NET:
    1. Start Microsoft Visual Studio .NET.
    2. On the File menu, point to New, and then click Project.
    3. 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.
  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 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("Panel" & 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
    					
  7. 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
    					
  8. Press F5 to run the application.
back to the top

Modification Type:MajorLast Reviewed:3/24/2004
Keywords:kbHOWTOmaster KB319312 kbAudDeveloper