Sunday 27 April 2008

How to switch between .H and .CPP files in visual studio.

I've written a macro for this, since I don't know any existing commands in VS.

Sub SwitchBetweenHeaderAndSource()
Dim oldFile As String
Dim newFile As String
oldFile = DTE.ActiveDocument.FullName
If (oldFile.EndsWith(".cpp")) Then
newFile = oldFile.Replace(".cpp", ".h")
Else
If (oldFile.EndsWith(".h")) Then
newFile = oldFile.Replace(".h", ".cpp")
Else
newFile = oldFile
End If
End If
Dim pi As EnvDTE.ProjectItem
pi = DTE.Solution.FindProjectItem(newFile)
If (Not pi Is Nothing) Then
pi.Open().Activate()
End If
End Sub

You can write this macro in macro explorer, which you can find in Tools->Macros menu.
To assign a keyboard shortcut, you can go to Tools->Customize and click the Keyboard button. On that window you can find the macro and assign it a keyboard shortcut. I'm using Ctrl+"

No comments: