C++ C++ C# C# ASP.NET Security ASP.NET Security ASM ASM Скачать Скачать Поиск Поиск Хостинг Хостинг  
  Программа для работы с LPT портом...
Язык: .NET — ©Alexey...
  "ASP.NET Atlas" – AJAX в исполнении Micro...
Язык: .NET — ©legigor@mail.ru...
  "Невытесняющая" Многопоточность...
Язык: C/C++ — ©...
  Update World C++: Сборник GPL QT исходников
  Весь сайт целиком можно загрузить по ссылкам из раздела Скачать
Дебетовая карта Home Credit [CPS] RU

 Adding Menu Items to context menus inside the Visual Studio .NET IDE / VS.NET Addins / C#

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

Дебетовая карта Home Credit [CPS] RU