StatusBarDrawItemEventHandler Delegate


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Represents the method that will handle the DrawItem event of a StatusBar.

Namespace:   System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public delegate void StatusBarDrawItemEventHandler(
	object sender,
	StatusBarDrawItemEventArgs sbdevent


Type: System.Object

The source of the event.

Type: System.Windows.Forms.StatusBarDrawItemEventArgs

A StatusBarDrawItemEventArgs that contains the event data.

When you create a StatusBarDrawItemEventHandler delegate, you identify the method that will handle the event. To associate the event with your event handler, add an instance of the delegate to the event. The event handler is called whenever the event occurs, unless you remove the delegate. For more information about event handler delegates, see Handling and Raising Events.

The following code example demonstrates how to use the StatusBarPanel.Style property, the StatusBarDrawItemEventHandler delegate, the StatusBarDrawItemEventArgs class, the StatusBarPanelStyle enumeration and the Panel property. To run the example, paste the following code in a form. Call the InitializeStatusBarPanels method in the form's constructor or Load event-handling method.

private StatusBar StatusBar1;

private void InitializeStatusBarPanels()
	StatusBar1 = new StatusBar();

	// Create two StatusBarPanel objects.
	StatusBarPanel panel1 = new StatusBarPanel();
	StatusBarPanel panel2 = new StatusBarPanel();

	// Set the style of the panels.  
	// panel1 will be owner-drawn.
	panel1.Style = StatusBarPanelStyle.OwnerDraw;

	// The panel2 object will be drawn by the operating system.
	panel2.Style = StatusBarPanelStyle.Text;

	// Set the text of both panels to the same date string.
	panel1.Text = System.DateTime.Today.ToShortDateString();
	panel2.Text = System.DateTime.Today.ToShortDateString();

	// Add both panels to the StatusBar.

	// Make panels visible by setting the ShowPanels 
	// property to True.
	StatusBar1.ShowPanels = true;

	// Associate the event-handling method with the DrawItem event 
	// for the owner-drawn panel.
	StatusBar1.DrawItem += 
		new StatusBarDrawItemEventHandler(DrawCustomStatusBarPanel);


// Draw the panel.
private void DrawCustomStatusBarPanel(object sender, 
	StatusBarDrawItemEventArgs e)

	// Draw a blue background in the owner-drawn panel.
	e.Graphics.FillRectangle(Brushes.AliceBlue, e.Bounds);

	// Create a StringFormat object to align text in the panel.
	StringFormat textFormat = new StringFormat();

	// Center the text in the middle of the line.
	textFormat.LineAlignment = StringAlignment.Center;

	// Align the text to the left.
	textFormat.Alignment = StringAlignment.Far;

	// Draw the panel's text in dark blue using the Panel 
	// and Bounds properties of the StatusBarEventArgs object 
	// and the StringFormat object.
	e.Graphics.DrawString(e.Panel.Text, StatusBar1.Font, 
		Brushes.DarkBlue, new RectangleF(e.Bounds.X, 
		e.Bounds.Y, e.Bounds.Width, e.Bounds.Height), textFormat);


.NET Framework
Available since 1.1
Return to top