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.
OnConnection event handler will look like this:
CommandBar oCommandBar = applicationObject.CommandBars["Code Window"];
CommandBarPopup oPopup = (CommandBarPopup)
oPopup.Caption = "Get Command Bars";
CommandBarControl oControl =
oControl.Caption = "Get Command Bars";
oSubMenuItemHandler.Click += new
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
protected void oSubMenuItemHandler_Click(object CommandaBarControl,
ref bool handled,ref bool cancelDefault)
System.Collections.ArrayList oList =
foreach(CommandBar oCommandBar in applicationObject.CommandBars)
frmCommandBarList ofrmCommandBarList = new frmCommandBarList();
ofrmCommandBarList.SetListBoxList = oList;
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
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".