This documentation is archived and is not being maintained.

ToolStripMenuItem Constructor (String, Image, EventHandler, String)

Initializes a new instance of the ToolStripMenuItem class with the specified name that displays the specified text and image that does the specified action when the ToolStripMenuItem is clicked.

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

public ToolStripMenuItem(
	string text,
	Image image,
	EventHandler onClick,
	string name


Type: System.String

The text to display on the menu item.

Type: System.Drawing.Image

The Image to display on the control.

Type: System.EventHandler

An event handler that raises the Click event when the control is clicked.

Type: System.String

The name of the menu item.

The following code example demonstrates a use of the ToolStripMenuItem constructor.

// This code example demonstrates how to handle the Opening event. 
// It also demonstrates dynamic item addition and dynamic  
// SourceControl determination with reuse. 
class Form3 : Form
    // Declare the ContextMenuStrip control. 
    private ContextMenuStrip fruitContextMenuStrip;

    public Form3()
        // Create a new ContextMenuStrip control.
        fruitContextMenuStrip = new ContextMenuStrip();

        // Attach an event handler for the  
        // ContextMenuStrip control's Opening event.
        fruitContextMenuStrip.Opening += new System.ComponentModel.CancelEventHandler(cms_Opening);

        // Create a new ToolStrip control.
        ToolStrip ts = new ToolStrip();

        // Create a ToolStripDropDownButton control and add it 
        // to the ToolStrip control's Items collections.
        ToolStripDropDownButton fruitToolStripDropDownButton = new ToolStripDropDownButton("Fruit", null, null, "Fruit");

        // Dock the ToolStrip control to the top of the form.
        ts.Dock = DockStyle.Top;

        // Assign the ContextMenuStrip control as the  
        // ToolStripDropDownButton control's DropDown menu.
        fruitToolStripDropDownButton.DropDown = fruitContextMenuStrip;

        // Create a new MenuStrip control and add a ToolStripMenuItem.
        MenuStrip ms = new MenuStrip();
        ToolStripMenuItem fruitToolStripMenuItem = new ToolStripMenuItem("Fruit", null, null, "Fruit");

        // Dock the MenuStrip control to the top of the form.
        ms.Dock = DockStyle.Top;

        // Assign the MenuStrip control as the  
        // ToolStripMenuItem's DropDown menu.
        fruitToolStripMenuItem.DropDown = fruitContextMenuStrip;

        // Assign the ContextMenuStrip to the form's  
        // ContextMenuStrip property. 
        this.ContextMenuStrip = fruitContextMenuStrip;

        // Add the ToolStrip control to the Controls collection. 

        //Add a button to the form and assign its ContextMenuStrip.
        Button b = new Button();
        b.Location = new System.Drawing.Point(60, 60);
        b.ContextMenuStrip = fruitContextMenuStrip;

        // Add the MenuStrip control last. 
        // This is important for correct placement in the z-order. 

    // This event handler is invoked when the ContextMenuStrip 
    // control's Opening event is raised. It demonstrates 
    // dynamic item addition and dynamic SourceControl  
    // determination with reuse. 
    void cms_Opening(object sender, System.ComponentModel.CancelEventArgs e)
        // Acquire references to the owning control and item.
        Control c = fruitContextMenuStrip.SourceControl as Control;
        ToolStripDropDownItem tsi = fruitContextMenuStrip.OwnerItem as ToolStripDropDownItem;

        // Clear the ContextMenuStrip control's Items collection.

        // Check the source control first. 
        if (c != null)
            // Add custom item (Form)
            fruitContextMenuStrip.Items.Add("Source: " + c.GetType().ToString());
        else if (tsi != null)
            // Add custom item (ToolStripDropDownButton or ToolStripMenuItem)
            fruitContextMenuStrip.Items.Add("Source: " + tsi.GetType().ToString());

        // Populate the ContextMenuStrip control with its default items.

        // Set Cancel to false.  
        // It is optimized to true based on empty entry.
        e.Cancel = false;

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

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

.NET Framework

Supported in: 3.5, 3.0, 2.0