C# Programming: Context Menu on Windows Forms - Part 1

by Mohamed Saood Khan

Learn how to add context menus to Windows Forms at the design and runtime level using Microsoft Visual Studio.

What's a Context Menu ?

A context menu (also called contextual, shortcut, and popup or pop-up menu) is a list of menu items that appear or visually represented in the form of a GUI (Graphical User Interface) that execute a behavior or action when invoked.

A context menu (also called contextual, shortcut, and popup or pop-up menu) is a list of menu items that appear or visually represented in the form of a GUI (Graphical User Interface) that execute a behavior or action when invoked.

One of the most common ways to invoke a context menu is by using a mouse, operations such as a right mouse click or middle click mouse operation.

An important observation to make here is the Context menu items change depending on the context on which they are invoked. In the simplest terms the context menu items change depending on the context they are invoked.

Figure shows the context menu being invoked when right clicked on the empty area of a desktop

Figure shows the context menu being invoked when right clicked on a directory/folder

Adding a Context Menu to windows forms at the design and Runtime level (shown with a sample program) using Visual Studio

  • If you are really want a context menu or a pop up menu show up in your application, almost all GUI frameworks provide a set of libraries to invoke context menu's

  • Editors like Visual Studio .Net 2003 and its related siblings allow the programmers to add Context Menu as objects to the application. You can simple access the objects properties to program its behavior statically.

Here's how you program it at the design level

  1. Start up Visual Studio .Net 2003 and select windows application (you can choose any language you are comfortable to program with). You should see something like the image below
    Fill in the necessary details, like the Name of your project and the location and click OK

  2. The next thing that should immediately show up in the editor is a windows form
  3. A quick description of what you are seeing in the above image.
    • On the left is the Visual Studio Tool Box. This is the place where you will find almost all windows controls that you can drag and drop them as objects on windows forms.

    • Towards your right is the Object Properties box. An important thing to understand here is that Visual Studio treats every control as objects that have behavior, state and events. The state of the controls can be set using the Property pane at the design level or at runtime.

    • In the middle you have the designer window displaying the basic windows form control that would be used as a starting point of an application. The form control acts as a container holding all other controls for use.

  4. From the tool box select Context Menu control (This control is available under Windows Forms category). Drag and Drop the control on the designer view. You should see something like this.


  5. Click on the Context Menu and start filling your Context Menu Items


  6. When you drag and drop a control from the Tool box, Visual Studio automatically sets a default name for the control. Most of the times the naming convention followed are the type-of-control suffixed with an integer value. So in our case since we selected a ContextMenu control, the default name would contextMenu1. The naming convention followed is the camel case (first char in lower case followed by an Upper case character for every word used in the name).

  7. We are almost done here. All you have to do is attach the context menu control to the form control that got created when we created a new windows application. To do this you need to highlight the form control (click on an area of the form to highlight it). From the properties pane select Context menu and attach the context menu that we added to the form.


  8. Run the application and see the context menu pop up when you right click on an area of the windows form.


So far we have learn't how to invoke a context menu on simple windows form at the design level. However you can also achieve the same behavior programmatically at runtime.

  1. Fire up Visual Studio .Net 2003 and create a new project. Select Windows application and the language of your choice.

  2. When you have created a new windows application project as already mentioned you would see a windows form control in the designer view.

  3. This time we are not going to select any Context Menu control from the tool box, instead we would program it to show up dynamically when you run the application

  4. In the designer, double click on the windows form control, you will notice that the designer view changes to become a source code editor. This has been pretty cool feature added in Visual Studio which separates the actual business logic from the design.

  5. When your view changes to the code editor, the first thing you will notice the Visual Studio has already created a skeleton or a basic framework containing methods. The two most visible methods are
    static void Main() 
    Application.Run(new Form1());
    private void Form1_Load(object sender, System.EventArgs e) 
  6. Like any other application, there is always a starting point where the application is executed from. In Java, this entry point is called the "main(String[] args)" method, in Windows Applications using Visual Studio its also called the "Main()"
    The second function is the Form1_Load(..) function. This is actually an event called when the form gets loaded after running the application.

  7. To invoke our context menu dynamically we would need to write some code. The best place to put in your code is in the Form1_Load() method. The reason is by the time the control reaches this function, the necessary initializations would have already been done to carry out the main business logic.

  8. Since we are have not dragged and dropped an object of the context menu, in order to invoke it at runtime we need to create an instance of the ContextMenu control class and its related menu items. Here's how we would do it.
  9. private void Form1_Load(object sender, System.EventArgs e)
     1    ContextMenu contextMenu1  = new ContextMenu(); 
     2    MenuItem menuItem1 = new MenuItem(); 
     3    MenuItem menuItem2 = new MenuItem(); 
     4    MenuItem menuItem3 = new MenuItem(); 
     5    contextMenu1.MenuItems.AddRange(new MenuItem[] {menuItem1, menuItem2,
     6    menuItem1.Index = 0; 
     7    menuItem1.Text = "Item1"; 
     8    menuItem2.Index = 1; 
     9    menuItem2.Text = "Item2"; 
    10    menuItem3.Index = 2; 
    11    menuItem3.Text = "Item3"; 
    12    this.ContextMenu = contextMenu1;
  10. Source code explanation
    • Line 1: Create an object of Context Menu class
      Note: Context Menu class is part of System.Windows.Form library. It would be easy to invoke these classes by importing the library instead of having to use the full namespace every time. Technically you would not have to worry about the library being imported, Visual Studio editor automatically imports the necessary libraries depending on the project created. Here's what you should see, the list of libraries imported by default by the editor.
      using System;
      using System.Drawing;
      using System.Collections;
      using System.ComponentModel;
      using System.Windows.Forms;
      using System.Data;
    • Line 2-4: Create an object of MenuItem class.

    • Line 5: Add all the menuitems to the context menu object.

    • Line 6-11: Specifying the context menu item names and there index positions.

    • Line 12: Attaching the context menu to the form.
      Note: In the previous section we did the same thing by using the properties pane for the windows form.
  11. We are all done now, execute the app and you should see something like this


In the next installment, we'll see how Windows Explorer content menus work, how to add them and how they differ from Windows Forms context menus.

This article was originally published on Wednesday Jul 21st 2010
Mobile Site | Full Site