ToolStripMenuItem Constructors

Definition

Initializes a new instance of the ToolStripMenuItem class.

Overloads

ToolStripMenuItem()

Initializes a new instance of the ToolStripMenuItem class.

ToolStripMenuItem(Image)

Initializes a new instance of the ToolStripMenuItem class that displays the specified Image.

ToolStripMenuItem(String)

Initializes a new instance of the ToolStripMenuItem class that displays the specified text.

ToolStripMenuItem(String, Image)

Initializes a new instance of the ToolStripMenuItem class that displays the specified text and image.

ToolStripMenuItem(String, Image, EventHandler)

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

ToolStripMenuItem(String, Image, ToolStripItem[])

Initializes a new instance of the ToolStripMenuItem class that displays the specified text and image and that contains the specified ToolStripItem collection.

ToolStripMenuItem(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.

ToolStripMenuItem(String, Image, EventHandler, Keys)

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

ToolStripMenuItem()

Source:
ToolStripMenuItem.cs
Source:
ToolStripMenuItem.cs
Source:
ToolStripMenuItem.cs

Initializes a new instance of the ToolStripMenuItem class.

C#
public ToolStripMenuItem();

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

ToolStripMenuItem(Image)

Source:
ToolStripMenuItem.cs
Source:
ToolStripMenuItem.cs
Source:
ToolStripMenuItem.cs

Initializes a new instance of the ToolStripMenuItem class that displays the specified Image.

C#
public ToolStripMenuItem(System.Drawing.Image image);
C#
public ToolStripMenuItem(System.Drawing.Image? image);

Parameters

image
Image

The Image to display on the control.

Remarks

Use the ToolStripMenuItem constructor to create a ToolStripMenuItem that displays an image in the margin alongside the text.

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

ToolStripMenuItem(String)

Source:
ToolStripMenuItem.cs
Source:
ToolStripMenuItem.cs
Source:
ToolStripMenuItem.cs

Initializes a new instance of the ToolStripMenuItem class that displays the specified text.

C#
public ToolStripMenuItem(string text);
C#
public ToolStripMenuItem(string? text);

Parameters

text
String

The text to display on the menu item.

Examples

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

C#
// Create a MenuStrip control with a new window.
MenuStrip ms = new MenuStrip();
ToolStripMenuItem windowMenu = new ToolStripMenuItem("Window");
ToolStripMenuItem windowNewMenu = new ToolStripMenuItem("New", null, new EventHandler(windowNewMenu_Click));
windowMenu.DropDownItems.Add(windowNewMenu);
((ToolStripDropDownMenu)(windowMenu.DropDown)).ShowImageMargin = false;
((ToolStripDropDownMenu)(windowMenu.DropDown)).ShowCheckMargin = true;

// Assign the ToolStripMenuItem that displays 
// the list of child forms.
ms.MdiWindowListItem = windowMenu;

// Add the window ToolStripMenuItem to the MenuStrip.
ms.Items.Add(windowMenu);

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

// The Form.MainMenuStrip property determines the merge target.
this.MainMenuStrip = ms;

Remarks

Use the ToolStripMenuItem constructor to create a ToolStripMenuItem with the specified name.

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

ToolStripMenuItem(String, Image)

Source:
ToolStripMenuItem.cs
Source:
ToolStripMenuItem.cs
Source:
ToolStripMenuItem.cs

Initializes a new instance of the ToolStripMenuItem class that displays the specified text and image.

C#
public ToolStripMenuItem(string text, System.Drawing.Image image);
C#
public ToolStripMenuItem(string? text, System.Drawing.Image? image);

Parameters

text
String

The text to display on the menu item.

image
Image

The Image to display on the control.

Remarks

Use the ToolStripMenuItem constructor to create a ToolStripMenuItem with the specified name and image.

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

ToolStripMenuItem(String, Image, EventHandler)

Source:
ToolStripMenuItem.cs
Source:
ToolStripMenuItem.cs
Source:
ToolStripMenuItem.cs

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

C#
public ToolStripMenuItem(string text, System.Drawing.Image image, EventHandler onClick);
C#
public ToolStripMenuItem(string? text, System.Drawing.Image? image, EventHandler? onClick);

Parameters

text
String

The text to display on the menu item.

image
Image

The Image to display on the control.

onClick
EventHandler

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

Examples

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

C#
// Create a MenuStrip control with a new window.
MenuStrip ms = new MenuStrip();
ToolStripMenuItem windowMenu = new ToolStripMenuItem("Window");
ToolStripMenuItem windowNewMenu = new ToolStripMenuItem("New", null, new EventHandler(windowNewMenu_Click));
windowMenu.DropDownItems.Add(windowNewMenu);
((ToolStripDropDownMenu)(windowMenu.DropDown)).ShowImageMargin = false;
((ToolStripDropDownMenu)(windowMenu.DropDown)).ShowCheckMargin = true;

// Assign the ToolStripMenuItem that displays 
// the list of child forms.
ms.MdiWindowListItem = windowMenu;

// Add the window ToolStripMenuItem to the MenuStrip.
ms.Items.Add(windowMenu);

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

// The Form.MainMenuStrip property determines the merge target.
this.MainMenuStrip = ms;

Remarks

Use the ToolStripMenuItem to create a ToolStripMenuItem that displays both text and an image and that does the specified action when the ToolStripMenuItem is clicked.

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

ToolStripMenuItem(String, Image, ToolStripItem[])

Source:
ToolStripMenuItem.cs
Source:
ToolStripMenuItem.cs
Source:
ToolStripMenuItem.cs

Initializes a new instance of the ToolStripMenuItem class that displays the specified text and image and that contains the specified ToolStripItem collection.

C#
public ToolStripMenuItem(string text, System.Drawing.Image image, params System.Windows.Forms.ToolStripItem[] dropDownItems);
C#
public ToolStripMenuItem(string? text, System.Drawing.Image? image, params System.Windows.Forms.ToolStripItem[]? dropDownItems);

Parameters

text
String

The text to display on the menu item.

image
Image

The Image to display on the control.

dropDownItems
ToolStripItem[]

The menu items to display when the control is clicked.

Remarks

Use the ToolStripMenuItem constructor to create a ToolStripMenuItem that displays text and an image and that contains the specified ToolStripItem collection.

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

ToolStripMenuItem(String, Image, EventHandler, String)

Source:
ToolStripMenuItem.cs
Source:
ToolStripMenuItem.cs
Source:
ToolStripMenuItem.cs

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.

C#
public ToolStripMenuItem(string text, System.Drawing.Image image, EventHandler onClick, string name);
C#
public ToolStripMenuItem(string? text, System.Drawing.Image? image, EventHandler? onClick, string? name);

Parameters

text
String

The text to display on the menu item.

image
Image

The Image to display on the control.

onClick
EventHandler

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

name
String

The name of the menu item.

Examples

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

C#
// 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");
        ts.Items.Add(fruitToolStripDropDownButton);

        // 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");
        ms.Items.Add(fruitToolStripMenuItem);

        // 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.
        this.Controls.Add(ts);

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

        // Add the MenuStrip control last.
        // This is important for correct placement in the z-order.
        this.Controls.Add(ms);
    }

    // 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.
        fruitContextMenuStrip.Items.Clear();

        // 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.
        fruitContextMenuStrip.Items.Add("-");
        fruitContextMenuStrip.Items.Add("Apples");
        fruitContextMenuStrip.Items.Add("Oranges");
        fruitContextMenuStrip.Items.Add("Pears");

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

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

ToolStripMenuItem(String, Image, EventHandler, Keys)

Source:
ToolStripMenuItem.cs
Source:
ToolStripMenuItem.cs
Source:
ToolStripMenuItem.cs

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

C#
public ToolStripMenuItem(string text, System.Drawing.Image image, EventHandler onClick, System.Windows.Forms.Keys shortcutKeys);
C#
public ToolStripMenuItem(string? text, System.Drawing.Image? image, EventHandler? onClick, System.Windows.Forms.Keys shortcutKeys);

Parameters

text
String

The text to display on the menu item.

image
Image

The Image to display on the control.

onClick
EventHandler

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

shortcutKeys
Keys

One of the values of Keys that represents the shortcut key for the ToolStripMenuItem.

Remarks

Use the ToolStripMenuItem constructor to create a ToolStripMenuItem that displays text and an image, performs the specified action when clicked, and has the specified shortcut key.

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10