Introduction to Windows Forms

Context Menus

A context menu is a floating menu that can pop up wherever it's needed for selections in a particular editing or user interface context. The convention is for the context menu to appear in response to a right-mouse button click.

Windows Forms follows this convention. The application has a ContextMenu property that, like its MainMenu counterpart, is available from the top-level application form.

Adding, showing, or modifying the appearance of a context menu and specifying its handler are largely the same as the main menu. The following adds a context menu with three items to the main window of the resize.cs form:

ContextMenu cmenu = new ContextMenu();
cmenu.MenuItems.Add(new MenuItem("&First"));
cmenu.MenuItems.Add(new MenuItem("&Second"));
cmenu.MenuItems.Add(new MenuItem("-"));
cmenu.MenuItems.Add(new MenuItem("&Third"));
this.ContextMenu=cmenu;

A simple right mouse click will bring up the context menu.

You might also like...

Comments

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“C++ : Where friends have access to your private members.” - Gavin Russell Baker