What is the easiest way to add a Visual Studio 2008 context menu item?

I would like to add a custom menu item when you right-click on a specific file extension in Visual Studio.

There seem to be some open source helper projects for this, but I would like to ask, has anyone ever used them, and how easy were they - and can you help me and give a starting point?

One of my research: http://www.codeplex.com/ManagedMenuExtension

+4
source share
2 answers

Here is a tutorial that explains how to add a context menu using a macro instead of creating a Visual Studio add-in. Hope this helps:

Visual Studio context menu extension

+5
source

Yes, the easiest way is to create a custom macro to handle your task (in VB).

Adding a Macro

First of all, choose Tools> Macros> Macros IDE (Alt + F11). To make everything clear, add a new module, for example, "ContextMenu", and paste the following code into it:

Imports System Imports EnvDTE Imports EnvDTE80 Imports EnvDTE90 Imports System.Diagnostics Public Module ContextMenu Public Sub DoSomething() 'Few declarations' Dim SolutionExplorer As UIHierarchy Dim Item As UIHierarchyItem Dim SelectedItem As EnvDTE.ProjectItem 'Getting the solution explorer' SolutionExplorer = DTE.Windows.Item(Constants.vsext_wk_SProjectWindow).Object() 'Iterating through all selected items' For Each Item In SolutionExplorer.SelectedItems 'Getting the item' SelectedItem = CType(Item.Object, EnvDTE.ProjectItem) 'Do some stuff here' If SelectedItem.FileNames(1).EndsWith("txt") Then MsgBox("We got the text file!", , SelectedItem.FileNames(1)) Else MsgBox("We got something else...", , SelectedItem.FileNames(1)) End If Next End Sub End Module 

Of course, you must set up a way to handle the selected file names. For now, it will show a popup for each file, otherwise if it is a txt file.

Setting the context menu

The second task is to add a custom macro to the context menu; go to: Tools> Settings

Mark the context menus from the list on the "Toolbars" tab (a new toolbar with all context menus should appear in the main window) and go to the "Commands" tab. Now, from the context menu bar, select "Project and Solution Context Menus"> Item and drag the macro onto it from the "Commands" tab. Change its name / icon / button in the right-click menu.

Now you are ready to check and use it. Your newly added macro should appear in the Item context menu. Have fun!

+10
source

All Articles