Export (0) Print
Expand All

StatusBar.DrawItem Event

Occurs when a visual aspect of an owner-drawn status bar control changes.

[Visual Basic]
Public Event DrawItem As StatusBarDrawItemEventHandler
[C#]
public event StatusBarDrawItemEventHandler DrawItem;
[C++]
public: __event StatusBarDrawItemEventHandler* DrawItem;

[JScript] In JScript, you can handle the events defined by a class, but you cannot define your own.

Event Data

The event handler receives an argument of type StatusBarDrawItemEventArgs containing data related to this event. The following StatusBarDrawItemEventArgs properties provide information specific to this event.

Property Description
BackColor (inherited from DrawItemEventArgs) Gets the background color of the item that is being drawn.
Bounds (inherited from DrawItemEventArgs) Gets the rectangle that represents the bounds of the item that is being drawn.
Font (inherited from DrawItemEventArgs) Gets the font assigned to the item being drawn.
ForeColor (inherited from DrawItemEventArgs) Gets the foreground color of the of the item being drawn.
Graphics (inherited from DrawItemEventArgs) Gets the graphics surface to draw the item on.
Index (inherited from DrawItemEventArgs) Gets the index value of the item that is being drawn.
Panel Gets the StatusBarPanel to draw.
State (inherited from DrawItemEventArgs) Gets the state of the item being drawn.

Remarks

You can use this event to perform drawing operations in an owner-drawn StatusBar control. For example, if you display an owner-drawn StatusBarPanel that displays a progress bar, you can use this event to perform the drawing of the progress bar on the panel. The data provided to the event through the StatusBarDrawItemEventArgs object passed as a parameter to the event handler enables you to determine the panel that needs to be drawn and the Graphics to use to draw to the panel. This event is only raised when the StatusBarPanel.Style property of a StatusBarPanel in a StatusBar control is set to OwnerDraw.

For more information about handling events, see Consuming Events.

Example

[Visual Basic, C#, C++] The following example demonstrates how to create an owner-drawn StatusBarPanel that displays a custom background and the current date. This example assumes that you have connected the DrawItem event of a StatusBar control to the event handler defined in the example.

[Visual Basic] 
Private Sub StatusBar1_DrawItem(ByVal sender As Object, ByVal sbdevent As System.Windows.Forms.StatusBarDrawItemEventArgs) Handles StatusBar1.DrawItem

   ' Create a StringFormat object to align text in the panel.
   Dim sf As New StringFormat()
   ' Format the String of the StatusBarPanel to be centered.
   sf.Alignment = StringAlignment.Center
   sf.LineAlignment = StringAlignment.Center

   ' Draw a back blackground in owner-drawn panel.
   sbdevent.Graphics.FillRectangle(Brushes.Black, sbdevent.Bounds)
   ' Draw the current date (short date format) with white text in the control's font.
   sbdevent.Graphics.DrawString(DateTime.Today.ToShortDateString(), StatusBar1.Font, Brushes.White, _
         New RectangleF(sbdevent.Bounds.X, sbdevent.Bounds.Y, _
         sbdevent.Bounds.Width, sbdevent.Bounds.Height), sf)
End Sub

[C#] 
private void DrawMyPanel(object sender, System.Windows.Forms.StatusBarDrawItemEventArgs sbdevent)
{
   // Create a StringFormat object to align text in the panel.
   StringFormat sf = new StringFormat();
   // Format the String of the StatusBarPanel to be centered.
   sf.Alignment = StringAlignment.Center;
   sf.LineAlignment = StringAlignment.Center;

   // Draw a back blackground in owner-drawn panel.
   sbdevent.Graphics.FillRectangle(Brushes.Black, sbdevent.Bounds);
   // Draw the current date (short date format) with white text in the control's font.
   sbdevent.Graphics.DrawString(DateTime.Today.ToShortDateString(), 
      statusBar1.Font,Brushes.White,sbdevent.Bounds,sf);
}

[C++] 
private:
    void DrawMyPanel(Object* /*sender*/, System::Windows::Forms::StatusBarDrawItemEventArgs* sbdevent) {
        // Create a StringFormat object to align text in the panel.
        StringFormat* sf = new StringFormat();
        // Format the String of the StatusBarPanel to be centered.
        sf->Alignment = StringAlignment::Center;
        sf->LineAlignment = StringAlignment::Center;

        // Draw a back blackground in owner-drawn panel.
        sbdevent->Graphics->FillRectangle(Brushes::Black, sbdevent->Bounds);
        // Draw the current date (short date format) with white text in the control's font.
        sbdevent->Graphics->DrawString(DateTime::Today.ToShortDateString(),
            statusBar1->Font, Brushes::White, RectangleF::op_Implicit(sbdevent->Bounds), sf);
    }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

StatusBar Class | StatusBar Members | System.Windows.Forms Namespace | OnDrawItem | StatusBarDrawItemEventArgs | StatusBarDrawItemEventHandler

Show:
© 2014 Microsoft