Click here to Skip to main content
12,897,242 members (48,513 online)
Click here to Skip to main content
Add your own
alternative version


38 bookmarked
Posted 14 Nov 2005

Adding Menu Items to context menus inside the Visual Studio .NET IDE

, 14 Nov 2005 CPOL
Rate this:
Please Sign up or sign in to vote.
This article describes how to add our custom menu items and sub menuitems to the context menu in the code window of the VS .NET IDE.

Sample Image


More often than not it is desirable to integrate our custom functionality into the development environment. Add-ins are a powerful way to do it. Once I had a requirement in my project to add a menu item to the context menu in the code editor of the IDE. So in this section, I will discuss about how to add a menu item to the context menu in the IDE.


I assume that you have sufficient expertise in creating add-ins for Visual Studio.

Using the code

Open a Visual Studio Add-in project in the IDE. This will take you through a wizard. Follow the default settings provided in the wizard. You will have an option to enter the Name and Description of the Add-in that we are going to create. I have given the name of the Add-in as CodeProjectAddin and a description of our requirement. This is the name that will appear in the Add-in Manager. Fill in the Name and Description fields with proper values. In the next screen you will have the option to choose the add-in options. See the image below to configure this wizard screen. Actually we are not going to add this add-in to our Tools menu item. This sample is going to add a menu item to the context menu in the code window. So check the option to load the add-in in the start-up and proceed.

Once you complete the wizard, a project is created with a class in Connect.cs.

There is an event handler named OnConnection() in the Connect.cs. This is the event, which gets fired when we make our add-in load on startup. So we have to write our code here to achieve our functionality.

My OnConnection event handler will look like this:

// Get the CommandBar for the window in which
// you want to add the menuitem. I am going to add it to the code window.
CommandBar oCommandBar = applicationObject.CommandBars["Code Window"];

// Note: The expression Code Window has to be used exactly.
// This is the .Net will identify
// the window to which we are going to add the Add=in.
// I will list down all the collection
// of CommandBars in my application later.

// I am going to add a MenuItem and a submenuitem to that.
// So I go ahead and create a PopUp Item.

CommandBarPopup oPopup = (CommandBarPopup) 
// Set the caption of the menuitem
oPopup.Caption = "Get Command Bars";

// Now I go ahead and add a Submenu item to the added Menuitem.

CommandBarControl oControl = 
// Set the caption of the submenuitem
oControl.Caption = "Get Command Bars";

// Now that we have added the menu items,
// we will associate the click events for these items.
// I will associate a click event only for the SubMenuitem at present.

oSubMenuItemHandler = 
oSubMenuItemHandler.Click += new 

// we can create a click event for the menuitem in the same way.

The event handler for the sub menu item click event will look like this. To handle the click event I have added a Windows Forms control and populated the listbox with the Commandbars collection.

//The event handler associated with the sub menu item.
CommandBarEvents oSubMenuItemHandler;

// event handler for Click event.
protected void oSubMenuItemHandler_Click(object CommandaBarControl, 
                           ref bool handled,ref bool cancelDefault)

    System.Collections.ArrayList oList = 
           new System.Collections.ArrayList();
    // I am getting all the command bar items
    // in the command bar collection.
    foreach(CommandBar oCommandBar in applicationObject.CommandBars)
    frmCommandBarList ofrmCommandBarList = new frmCommandBarList();
    ofrmCommandBarList.SetListBoxList = oList;

    // We have to use the exact term as listed
    // in the listbox, if we want to add
    // the context menu to some other window.

Compile the source code and install the set up. To select this add-in, go to Tools -> Add-in Manager.

The add-in we created will be displayed. Click the check box next to it. This will enable your add-in. Now right click on the code editor window. A new menu item would have been added. Click the submenu item. A Windows application is launched and this will list down all the command bars available in the CommandBars collection.

Points of Interest

When we want to add the menu item to any other window, say for example a SQL Query pane window in a database project, we have to get the exact item from the command bars collection i.e., "Query SQL Pane".

Happy coding..


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

No Biography provided

You may also be interested in...

Comments and Discussions

QuestionShort and nice one.. Pin
Shankarcodebase9-Oct-13 2:25
memberShankarcodebase9-Oct-13 2:25 
QuestionGUI controls context menu "Selection" Pin
Michael Breuer19-Aug-13 21:16
memberMichael Breuer19-Aug-13 21:16 
QuestionHow to deploy AddIn Project Pin
nguyennhutoan03ct26-Jun-07 18:21
membernguyennhutoan03ct26-Jun-07 18:21 
GeneralHey thanks for the info Pin
icestatue5-Apr-06 8:48
membericestatue5-Apr-06 8:48 
Jokea better way Pin
ET200513-Dec-05 22:39
memberET200513-Dec-05 22:39 
GeneralRe: a better way Pin
Abhayc9-Jun-06 2:57
memberAbhayc9-Jun-06 2:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170424.1 | Last Updated 14 Nov 2005
Article Copyright 2005 by Saravanan Suriyanarayanan
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid