I would like to add a custom menu item when you right-click a certain file extension in Visual Studio.
There seem to be some helper open source projects to accomplish this, but I'd like to ask if anyone has ever used them, and how easy were they - and can you help me and provide a starting point?
One I've researched is: http://www.codeplex.com/ManagedMenuExtension
Yeah, the easiest way is to create custom macro to handle your task (in VB).
First of all select Tools>Macros>Macros IDE (Alt+F11). To make everything clear, add a new module for example "ContextMenu" and put in it the following code:
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 have to customize the way you're handling selected filenames. For now, it will just show a popup for every file, different if it will be txt file.
The second task to do is to add your custom macro to the context menu; go to: Tools>Customize
Tick the context menus from the list on "Toolbars" tab (the new toolbar with all context menus should appear on main window) and switch to "Commands" tab. Now, from context menus toolbar select: "Project and Solution Context Menus">Item and drag your macro onto it from "Commands" tab. Change its name/icon/button under right click menu.
Now you are ready to test and use it. Your newly added macro should appear in Item context menu. Have a fun!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With