Wednesday, March 10, 2010

Add an Interface Using the Keyboard in Visual Studio

I realized long ago that using a keyboard-centric approach rather than a mouse-driven approach to coding is much more productive and efficient. As such, I thought it might be nice to share one keyboard function that isn’t currently available in Visual Studio – adding a new interface to a project.

Step 1: Create a new macro
Open Visual Studio and navigate to Tools > Macros > Marcos IDE… (or use the keyboard shortcut Alt+F11)

Step 2: Double-click one of the modules in the MyMacros project

Step 3: Past this code into the code window:

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

Note: You may need to change the path “Visual C# Items\Code\Interface” to point to your interface template.

Step 4: Assign the macro to a Keyboard shortcut:

Navigate to Tools > Options… and in the Environment section select Keboard (there is no shortcut key for this by default, but I added on Ctrl+W, K). In the “Show commands containing” text box, type MyMacros to see a list of all your macros. Select AddInterface (the new macro we just added). Put your mouse cursor into the “Press shortcut keys” text box and press the keystroke you wish to use to cause the macro to run (mine is Ctrl+Shift+A,I). Click the Assign button and then Ok.

That’s all there is to it. Now when you use the assigned keystroke, an input box will display asking for the name of your new interface, and it will be added to your project.

No comments:

Post a Comment