SIGN UP MEMBER LOGIN:    
ARTICLE

MenuStrip and ContextMenuStrip controls

Posted by Sivaraman Dhamodaran Articles | Windows Forms C# September 17, 2011
In this article we will look into the MenuStrip and ContextMenuStrip controls. We will also explore the improtance of Shortcut Keys and Alt+key combinations.
Reader Level:
Download Files:
 

1. Introduction to Menu Strips


Strip is a relatively narrow piece of something. Dot net has three important strip controls namely MenuStrip, StatusStrip and ToolStrip. In this article we will start with the menustrip control.


All the strips controls accommodate some other UI elements in it. A menu strips allows you to add Menu and Menu allows you to add menu items. Similarly the toolstrip control allows you add one or more tool bar buttons in it. OK, let us go to the MenuStrip. Look at the below screen shot:

 

Pic01.JPG

 

A Menubar, dot net calls it Menu strip can accommodate multiple Menus. In the above picture only File menu is added to it. Menu items are usually added to the menu. Say for example in the above picture, the Exit and Just for Demo are menu items. Menu items are usually linked to the command handler and when you click 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. The above picture shows one such sub-menu with two menu items in it.

 

In this example, we will create the above shown form with menu and menu items. Then we will provide some simple handler for it.

 

2. Adding menu Strips control

 

Video Link (C-sharpcorner blog): Add menu and Menu Items

 

The menu strip is grouped in the toolbox under the Menus & Toolbars group. This is shown in the below picture. To use menustrip in the form drag the menustrip from the toolbox and drop it to the form.

 

Pic02.JPG

 

In the below video, a menu strip is added to the form. To add a separator in the menu just type the hyphen (-) for the menu name. The placement ampersand (&) in the menu item name displays underline for letter next to it. This under line is displayed at runtime when alt key is pressed and typing the letter with underline (alt key is still kept down) will call the menu item handler. The usage of ampersand in the menu item with alt key is shown below:

 

Pic03.JPG

 

So in the above picture in place of clicking the Exit menu item, you can call the handler by using keys Alt+F,X

 

3. Adding Shortcut key


Video Link (C-sharpcorner blog): Adding Shortcut key

 

Short cut key is one more way to call the handler for menu item using the keyboard keys. This differs from the alt+key combination in such a way that Alt+key combination always opens the Menu to give a clue about the key combinations by displaying the underline for the letters. You may already observe it when doing Alt+FX combination. The short keys not even open the menu and when required combination met the handler for menu item gets called. The video shows adding the short cut key Ctrl+H to the menu item Say Hello. To assign the short cut key for the menu item, use the shortcutkeys property of the menu item.

 

4. Adding image to the Menu Item


Video Link (C-sharpcorner blog): Add Image Icon to Menu item

You 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 the required image is added to the resource. Then from the resource the image is picked and assigned to the menu item. The Image property of the menu item is used to assign the image to a menu item.

 

5. Adding context menu


Video Link (C-sharpcorner blog): Context menu support for the form

Video Link (C-sharpcorner blog): Adding Handler Code

 

The context menu can be added to the form the same way you added the MenuStrip control. This time we should add the ContextMenuStrip control to the form. Unlike menu strip control, the context menu can have only one Menu with one or more menu items in it. Menu items to the context menu are added in the way you do it for the menu strips. The important step is assigning the context menu to the form and you can do that by using the contextmenu property of the windows form.

 

Once the context menu is assigned to the form, during the runtime it can be displayed by right clicking the mouse in the form area. This is shown below:

 

Pic04.JPG

 

Note: Adding the handler code for menu item shown in the video is same for Menu strip also.

 

6. Code for All handlers


Hope explanation is not required as it is just a piece of code to check the handler is called or not.

 

//001_MenuStrips : Handlers for Menu Strips

private void sayHaiToolStripMenuItem_Click(object sender, EventArgs e)

{

    MessageBox.Show("Hai");

}

 

private void sayHelloToolStripMenuItem_Click(object sender, EventArgs e)

{

    MessageBox.Show("Hello");

}

 

private void exitToolStripMenuItem_Click(object sender, EventArgs e)

{

    this.Close();

}

 

// 002_MenuStrips: Handler for Context Menu

private void mnuItemExit_Click(object sender, EventArgs e)

{

    MessageBox.Show("Sample going to exit");

    this.Close();

}

Login to add your contents and source code to this article
share this article :
post comment
 
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Become a Sponsor