Export (0) Print
Expand All
Expand Minimize

DrawItemEventHandler Delegate

Represents the method that will handle the DrawItem event of a ComboBox, ListBox, MenuItem, or TabControl control.

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

public delegate void DrawItemEventHandler(
	Object sender,
	DrawItemEventArgs e
)

Parameters

sender
Type: System.Object

The source of the event.

e
Type: System.Windows.Forms.DrawItemEventArgs

A DrawItemEventArgs that contains the event data.

When you create a DrawItemEventArgs 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 a menu with an owner-drawn menu item. This example uses the AddHandler statement and the AddressOf operator to designate a delegate to handle the MenuItem.DrawItem event. To run the example place paste it in a form that imports the System, System.Windows.Forms, and System.Drawing namespaces. Ensure all events are associated with their event-handling methods.

	// Declare the MainMenu control. 
	internal System.Windows.Forms.MainMenu MainMenu1;

	// Declare MenuItem2 as With-Events because it will be user drawn. 
	internal System.Windows.Forms.MenuItem MenuItem2;


	private void InitializeMenu()
	{

		// Create MenuItem1, which will be drawn by the operating system.
		MenuItem MenuItem1 = new MenuItem("Regular Menu Item");

		// Create MenuItem2.
		MenuItem2 = new MenuItem("Custom Menu Item");

		// Set OwnerDraw property to true. This requires handling the 
		// DrawItem event for this menu item.
		MenuItem2.OwnerDraw = true;

		//Add the event-handler delegate to handle the DrawItem event.
        MenuItem2.DrawItem += new DrawItemEventHandler(DrawCustomMenuItem);
		
      // Add the items to the menu.
		MainMenu1 = new MainMenu(new MenuItem[]{MenuItem1, MenuItem2});																													  

		// Add the menu to the form. 
		this.Menu = this.MainMenu1;
	}

	// Draw the custom menu item. 
	private void DrawCustomMenuItem(object sender, 
		DrawItemEventArgs e)
	{

		// Cast the sender to MenuItem so you can access text property.
		MenuItem customItem = (MenuItem) sender;

		// Create a Brush and a Font to draw the MenuItem.
		System.Drawing.Brush aBrush = System.Drawing.Brushes.DarkMagenta;
		Font aFont = new Font("Garamond", 10, 
			FontStyle.Italic, GraphicsUnit.Point);

		// Get the size of the text to use later to draw an ellipse 
		// around the item.
		SizeF stringSize = e.Graphics.MeasureString(
			customItem.Text, aFont);

		// Draw the item and then draw the ellipse.
		e.Graphics.DrawString(customItem.Text, aFont, 
			aBrush, e.Bounds.X, e.Bounds.Y);
		e.Graphics.DrawEllipse(new Pen(System.Drawing.Color.Black, 2),
			new Rectangle(e.Bounds.X, e.Bounds.Y, 
			(System.Int32)stringSize.Width,
			(System.Int32)stringSize.Height));
	}
		owner-drawn menu item. This example uses the AddHandler statement and the AddressOf operator to designate a delegate to handle the DrawItem event.


	To run the example place paste it in a form that imports the System, System.Windows.Forms, and System.Drawing namespaces.

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft