Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

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

0.00/5 (No votes)
14 Nov 2005 1  
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

Introduction

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.

Background

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) 
  oCommandBar.Controls.Add(MsoControlType.msoControlPopup,
  System.Reflection.Missing.Value,
  System.Reflection.Missing.Value,1,true);
// 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 = 
  oPopup.Controls.Add(MsoControlType.msoControlButton,
  System.Reflection.Missing.Value,
  System.Reflection.Missing.Value,1,true);
// 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 = 
  applicationObject.Events.get_CommandBarEvents(oControl);
oSubMenuItemHandler.Click += new 
  _dispCommandBarControlEvents_ClickEventHandler(oSubMenuItemHandler_Click);

// 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)
    {
        oList.Add(oCommandBar.Name);
    }
    frmCommandBarList ofrmCommandBarList = new frmCommandBarList();
    ofrmCommandBarList.SetListBoxList = oList;
    ofrmCommandBarList.ShowDialog();

    // 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..

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here