C# ContextMenuStrip Example

by Sam Allen - Updated November 27, 2009

You want to add the ContextMenuStrip control to your Windows Forms program. Context menus should appear when your user right-clicks, reacting to the surroundings. Here we see the steps to create a ContextMenuStrip and use it with the C# language in your program.

ContextMenuStrip screenshot

Getting started

First, here is how to create ContextMenuStrip controls in your Windows Forms program. For the example, you may have a TextBox on the form, which you can name anything you want. We will call it textBox1 in this guide.

First, get oriented. Look at the Toolbox, which is usually a tab or panel on the left. Click on the Menus & Toolbars heading, and you will see a list of items. Next, select the ContextMenuStrip item. Double-click on the ContextMenuStrip item and then enter some text such as "Copy" in the gray box in the context menu.

Using the ContextMenuStrip property

Each Windows Forms control has a ContextMenuStrip property. The job of the Visual Studio Designer view is to allow us to visually set those properties. We can do this by following these steps.

First, click on control. First, click on the TextBox, textBox1, you may have on your form. On the right side of your Visual Studio window, you may have a Properties pane.

Select the property. There are buttons near the top, and you can select the A-Z listing. In the A-Z listing, you will see an alphabetized list of properties. We want the ContextMenuStrip property.

Lightning bolt in Visual Studio

Changing the Click event

We need to make the context menu actually do something. Here we make it perform a Copy on textBox1 when the user clicks on it. Go ahead and double click on your custom menu item, which will create an event handler that looks like this. We use the Copy method call.

private void copyToolStripMenuItem1_Click(object sender, EventArgs e)
{
    // Here, add the copy command to the relevant control, such as...
    textBox1.Copy(); // added manually
}

Use the Opening event

First, click on the ContextMenu and then look at Properties pane on the right. Then click on the lightning bolt in Properties pane. Finally, scroll down to Opening and double-click to the right of Opening. You will see the following code appear.

private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
{
    // Runs before the user sees anything. A great place to set
    // Enabled to true or false.
}

Adding to event

Here we change the contents of the event handler. We can set the Copy menu item as Enabled when the selection is present, and disabled when there is no selection. Modify the above code to look like the following code.

private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
{
    // An elegant way to make the ContextMenu item always be Enabled
    // when there is a selection, and always disabled when there isn't.
    copyToolStripMenuItem1.Enabled = textBox1.SelectionLength > 0;
}

What we've accomplished

We saw a way to disable the context menu's items depending on other factors in the UI. You won't need to manage the ToolStripMenuItem objects in any other way, other than the check for whether you want them in the Opening event. When text is selected, the Copy menu item is enabled.

Summary

Here we saw how you can use the ContextMenuStrip in your Windows Forms programs based on the C# programming language. ContextMenuStrip is a useful control and it can truly bring a higher level of interactivity to your Windows Forms programs. You can use StatusStrip for UI messages, in conjunction with ContextMenu.

(Do not copy this page.)

Dot Net Perls | Search
Windows Forms | BackgroundWorker Tutorial | DataGridView Tips and Secrets | DataGridView Tutorial | MessageBox.Show Examples in Windows Forms | TextBox Tutorial
C# | Integer.TryParse | ArrayList Examples | Bituminous Coal | Sleep Method Use
© 2009 Sam Allen. All rights reserved.
Dot Net Perls | Sam Allen