Programming Examples

Are you a Programmer or Application Developer or a DBA? Take a cup of coffee, sit back and spend few minutes here :)

C# MenuStrip & ContextMenuStrip Controls




1. About this MenuStrip Control Example

A Strip is a nearly thin slice of an object. Dot net has three great strip controls, namely MenuStrip, StatusStrip and ToolStrip. In this Example, we will explore the C# MenuStrip and ContextMenuStrip Controls.

All the strip controls accommodate some other UI elements in it. A menu strip allows us to add Menu and Menu allows us to add Menu Items. Similarly, the ToolStrip control allows us to add one or more Tool Bar buttons in it. OK, let us go to the MenuStrip. The below screenshot shows our example:


A Menu bar dot net calls it as a Menu strip can hold multiple Menus. In the above example form we add only File Menu to it. The Menu holds one or more Menu Items. Say for example in the above picture, the Exit and Just for Demo are Menu items. Menu items are hooked up to a command handler, and when the user clicks the Menu item, the corresponding handler gets called.

If a Menu item is linked with one more Menu, then the resulting Menu is called as Sub-Menu. For Example, in the above picture, we can see one such sub-Menu with two Menu items in it. In this example, we will create the above shown C# Windows Form with Menu and Menu items in the C# MenuStrip Control. Then we will provide some simple handler for it.

2. Adding Menu and Menu Items to MenuStrip Control

Microsoft Visual Studio groups the MenuStrip Control in the toolbox under the Menus & Toolbars group. The below picture shows this. To use Menustrip in the form, we have to drag the Menustrip from the toolbox and drop it to the form.

Location MenuStrip Control in Tool Box

Location MenuStrip Control in Tool Box

The video at the end of this section shows how one can add C# MenuStrip Control to the C# Windows Form. A Separator is a special menu item which shows an underline as menu item text. This is useful to group the Menu Items. To add a separator in the Menu, one must type the hyphen (-) for the Menu name. The placement of ampersand (&) in the Menu item name displays an underline in the letter next to it. We can see this underline by pressing and holding down the ALT key. Moreover, typing the letter with underline with ALT key still down will call the handler code hooked to the Menu item. The usage of ampersand in the Menu item with ALT key is shown below:

MenuStrip Control - Alt Key Processing

MenuStrip Control – Alt Key Processing

So, in the above picture in place of clicking the Exit Menu item, you can call the handler by using the keys Alt+F, X. The below video shows how we add MenuStrip to C# Form and then it shows adding Menu Items to File menu.

Video 1: Adding MenuStrip and Setting Properties [No Audio]

3. Adding Shortcut key to Menu Items

Shortcut Key is one more way to call the handler for Menu item using the keyboard keys. This differs from the alt+key sequence in such a way that Alt+key combination always opens the Menu to give a clue about the key sequences by displaying the underline for the letters. We may already look at it when we did Alt+F,X key stroke. The shortcut keys not even open the Menu. When required key combination met, Dotnet framework calls the handler for Menu item. To assign the shortcut key for a Menu item, we should set it through the


  property of the Menu item. The video below shows adding the shortcut key Ctrl+H to the Menu item Say Hello.

Video 2: Adding a Shortcut Key to a Menu item

4. Adding Icon to a Menu Item

One can find some useful images for Menu items in the path (It may vary in your system based on your installation path) Program Files\Microsoft Visual Studio 8\Common7\VS2005ImageLibrary\VS2005ImageLibrary\bitmaps. To add the image, first we add the required image as a application resource. Then from the resource one can pick the image and assign it to the Menu item. The “Image Property” of the


is used to assign the image or icon to a Menu item.

Video 3: Assigning an Icon to a Menu Item

5. Adding Context Menu

The Menu which gets displayed while user right clicks on the application is what called ‘Context Menu’. We can add the Context Menu to our form the same way we added the MenuStrip control. But this time we should pick the “ContextMenuStrip Control” from the toolbox and drop it to our form. Unlike MenuStrip control, the context Menu can have only one Menu with one or more Menu Items in it. We add menu items to the context Menu in the way we do it for the MenuStrip Control. The important step is assigning the context Menu to the form and you can do that by using the


property of the windows form.

Once the context Menu is assigned to the form, during the run-time, dot-net framework will display it when user right-clicks anywhere in the form. This is shown below:

C# Context Menu Example

C# Context Menu Example

Video 4: creating a Context Menu and Assigning it to Windows Forms

6. Adding Handler Functions for Menu Items

The video below shows adding a Handler function for the Menu Item.

Video 5: Adding a Handler Function for Menu Item

All the handler functions are below:

Download the Example from Google Drive: C# MenuStrip & ContextMenuStrip Example

Categories: C#

Tags: , , ,

1 reply

  1. What is the code for the Menustrip Edit – Cut Copy and Paste

Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.