September 12, 2008

Visual Studio "Add Interface" context menu

Sometimes it seems Visual Studio is set up for a completely different programming style than mine. For example the context menu's in the solution explorer has an "Add Class" item that I use often but there is no "Add interface" item. It's easy to customize Visual Studio so I set about adding this. Should be easy right? Right... I even had to write VBA to get it done, I feel dirty. But I got through it so you don't have to, enjoy!

I thought there should be a standard command for adding an interface. But there wasn't. Lets fire up the macro engine and type after me.

   1: Sub AddInterface()
   2:     Dim interfaceName As String = Microsoft.VisualBasic.Interaction.InputBox("Name", "Add Interface")
   3:     If Not interfaceName.ToLower.EndsWith(".cs") Then
   4:         interfaceName &= ".cs"
   5:     End If
   6:     DTE.ItemOperations.AddNewItem("Visual C# Items\Code\Interface", interfaceName)
   7: End Sub

Now lets fix the menu's, select the Tools menu and then Customize... This opens a window with two tabs. While this window is open you can edit all the Visual Studio toolbars by dragging and dropping icons.


image_14We can edit the context menus by selecting Context Menus on the Toolbars tab. Now an extra toolbar gets displayed with all the available context menus.


I rearranged my menu so it looks like the menu on the right. You can add separators in the menu's by selecting the item you want to appear under the bar and click Begin a Group in the context menu.


 


image_16Now we want to add the Add Interface item. For this we need the Commands tab of the Customize window. This tab contains all the commands you might want to add to your toolbars and menu's. Add Interface under the Project category doesn't do what we want it to do but luckilly we can select our macro. Adding it to our menu's is as simple as dragging it over. You'll probably want to rename the menu item to something that makes sense by right clicking it.


You might want to take a look at the other commands that are available. If you have trouble finding a command you can try the Keyboard... button in the lower left corner of the screen and search for it that way.


And if you're worried about messing things up, if you want to reset a single toolbar or menu to it's initial state you can select the toolbar in the Toolbars tab and click the Reset button.


I promise, no more VBA next time. I'll try to finish the last part of the Unit testing and PInvoking as soon as I have time.

No comments:

Post a Comment