C# ContextMenuStrip Example

Use the ContextMenuStrip control from Windows Forms. Handle the Opening event.

ContextMenuStrip. This enhances usability in programs. Context menus should appear when a user right-clicks, reacting to the surroundings.

Steps. We see the steps to create a ContextMenuStrip. Use ContextMenuStrip to create a custom menu on right-click events. It usually includes Copy, Cut and Paste.

Start: Here we show how to create ContextMenuStrip controls in your Windows Forms program.

TextBox: 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.

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.

Then: Double-click on the ContextMenuStrip item and then enter some text such as "Copy" in the gray box in the context menu.

Each Windows Forms control has a ContextMenuStrip property. The Visual Studio Designer view allows us to visually set those properties, without code statements.

Step 1. 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. Look at the entries of this pane.

Step 2. Select the ContextMenuStrip 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.

Click. 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.

Next: Go ahead and double click on your custom menu item, which will create an event handler that looks like this.

Code that implements Click event: C# private void copyToolStripMenuItem1_Click(object sender, EventArgs e) { // Here, add the copy command to the relevant control, such as... textBox1.Copy(); // Added manually. }

Open. You need to 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 in the user interface to the right of Opening.

And: You will see this block of code appear. It is generated by the Visual Studio environment.

Code that implements Opening event: C# 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.
More detailed opening event handler: C# 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; }

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.

When text is selected, the Copy menu item is enabled. This approach is reliable and easy to understand. ContextMenuStrip is a useful control.
Dot Net Perls
© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to