Click here to Skip to main content
15,897,891 members
Articles / Programming Languages / C#

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

Rate me:
Please Sign up or sign in to vote.
4.09/5 (5 votes)
14 Nov 2005CPOL2 min read 107.4K   1.6K   38   6
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.

Image 2

Image 3

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:

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

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

Image 4

Image 5

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionShort and nice one.. Pin
Shankarcodebase9-Oct-13 2:25
Shankarcodebase9-Oct-13 2:25 
QuestionGUI controls context menu "Selection" Pin
Michael Breuer19-Aug-13 21:16
Michael Breuer19-Aug-13 21:16 
QuestionHow to deploy AddIn Project Pin
nguyennhutoan03ct26-Jun-07 18:21
nguyennhutoan03ct26-Jun-07 18:21 
GeneralHey thanks for the info Pin
icestatue5-Apr-06 8:48
icestatue5-Apr-06 8:48 
Jokea better way Pin
ET200913-Dec-05 22:39
ET200913-Dec-05 22:39 
GeneralRe: a better way Pin
Abhayc9-Jun-06 2:57
Abhayc9-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.