Short-cut to switch between .h and .cpp file

  • Thread starter Thread starter Torben Laursen
  • Start date Start date
T

Torben Laursen

Hi

Can anyone tell me what is the short-cut to switch between a .h and .cpp
file, thanks

Torben Laursen
 
Torben Laursen said:
Hi

Can anyone tell me what is the short-cut to switch between a .h and .cpp
file, thanks

Torben Laursen
Can you elaborate on what you mean by switch?
Are you referring to the view in the editor window in Visual Studio .NET, or
to something else?
If it's viewing the files in the editor window, using the tabs at the top is
the quickest way to switch (once both files have been opened).
 
Hi Peter van der Goes,
If it's viewing the files in the editor window, using the tabs at the
top is the quickest way to switch (once both files have been opened).

The quickest way is "Ctrl-TAB" (so I do not have to catch the mouse...)

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
 
Jochen Kalmbach said:
Hi Peter van der Goes,


The quickest way is "Ctrl-TAB" (so I do not have to catch the mouse...)

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/

Thanks, Jochen. You're correct of course, with one caveat.
If I have 6 or 7 source code files open in the editor, ctrl-tab seems to
switch between files in the order they were opened, not necessarily in the
desired order.
 
Hi Peter van der Goes,
Thanks, Jochen. You're correct of course, with one caveat.
If I have 6 or 7 source code files open in the editor, ctrl-tab seems
to switch between files in the order they were opened, not necessarily
in the desired order.

No, it switches in the order, which was last used!

If you need to switch between two files, you *only* need to select the
second file once and then one Ctrl-TAB will switch between these two files.

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
 
Jochen said:
Hi Peter van der Goes,


No, it switches in the order, which was last used!

If you need to switch between two files, you *only* need to select the
second file once and then one Ctrl-TAB will switch between these two files.

Ctrl+Tab ain't it -- that is plain MDI window navigation.

What is needed is a way to switch (open if necessary!) between the .h and
..c/cpp file.

WndTabs (for VS6) provided this feature, and was immensely beneficial. I'm not
aware of a similar capability w/ VS03.
 
Jochen Kalmbach said:
Hi Peter van der Goes,

No, it switches in the order, which was last used!

If you need to switch between two files, you *only* need to select the
second file once and then one Ctrl-TAB will switch between these two files.

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/

But, not necessarily in the desired order, depending on what you've been
doing.
 
Julie said:
Ctrl+Tab ain't it -- that is plain MDI window navigation.

What is needed is a way to switch (open if necessary!) between the .h and
.c/cpp file.

WndTabs (for VS6) provided this feature, and was immensely beneficial. I'm not
aware of a similar capability w/ VS03.

Here is the macro that I came up with:

Sub OpenAssociatedSourceFile()
If ActiveDocument Is Nothing Then
Microsoft.VisualBasic.Beep()
Return
End If
If ActiveDocument.FullName Is Nothing Then
Microsoft.VisualBasic.Beep()
Return
End If
Dim FileName As String = ActiveDocument.FullName
If FileName.EndsWith(".cpp") Then
FileName = FileName.Substring(0, FileName.LastIndexOf(".")) + ".h"
ElseIf FileName.EndsWith(".h") Then
FileName = FileName.Substring(0, FileName.LastIndexOf(".")) +
".cpp"
Else
FileName = Nothing
End If
If System.IO.File.Exists(FileName) Then
DTE.ItemOperations.OpenFile(FileName)
Else
Microsoft.VisualBasic.Beep()
End If
End Sub


I then assigned it to Ctrl+Shift+H to switch between .cpp/h files.
 
Julie said:
Here is the macro that I came up with:

Sub OpenAssociatedSourceFile()
If ActiveDocument Is Nothing Then
Microsoft.VisualBasic.Beep()
Return
End If
If ActiveDocument.FullName Is Nothing Then
Microsoft.VisualBasic.Beep()
Return
End If
Dim FileName As String = ActiveDocument.FullName
If FileName.EndsWith(".cpp") Then
FileName = FileName.Substring(0,
FileName.LastIndexOf(".")) + ".h" ElseIf
FileName.EndsWith(".h") Then FileName =
FileName.Substring(0, FileName.LastIndexOf(".")) + ".cpp"
Else
FileName = Nothing
End If
If System.IO.File.Exists(FileName) Then
DTE.ItemOperations.OpenFile(FileName)
Else
Microsoft.VisualBasic.Beep()
End If
End Sub


I then assigned it to Ctrl+Shift+H to switch between .cpp/h files.

Thanks for posting this - I'm sure others will find it useful.

Personally, I've never wanted the feature. My C++ coding style rarely
results in matched .cpp/.h pairs, so I don't miss the feature. I do
remember this feature from way back in Turbo C/Borland C++ though.

-cd
 
Hi Peter van der Goes,
But, not necessarily in the desired order, depending on what you've been
doing.

"It switches in the order, which was last used"

Is this wrong !?

As Juli said: "This is plain MDI window navigation."

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
 
Carl Daniel said:
[...]

Thanks for posting this - I'm sure others will find it useful.

Yes, I would have, if Visual Assist wouldn't
have taken care of it already.
Personally, I've never wanted the feature. My C++ coding style rarely
results in matched .cpp/.h pairs, so I don't miss the feature.

It certainly doesn't match all the time for
me -- but rarely ever? What do you do then?
I do
remember this feature from way back in Turbo C/Borland C++ though.

Oh yeah, in BCB it was CTRL-F6 (as opposed
to CTRL-TAB which was normal MDI behaviour). :)


Schobi

--
(e-mail address removed) is never read
I'm Schobi at suespammers dot org

"The presence of those seeking the truth is infinitely
to be prefered to those thinking they've found it."
Terry Pratchett
 
Jochen Kalmbach said:
Hi Peter van der Goes,


"It switches in the order, which was last used"

Is this wrong !?

As Juli said: "This is plain MDI window navigation."
Practicing a little revisionist history?
I'm not arguing that point Jochen. You started this subthread by pointing
out that my comment on using the mouse to select the desired tab is not the
"fastest" way. My point is: given the standard behavior of Ctrl-Tab (I agree
that it is "... plain MDI window navigation."), using the mouse may well be
more efficient for some, depending on circumstances. To each his own, eh?
 
Hi Peter van der Goes,
I'm not arguing that point Jochen. You started this subthread by
pointing out that my comment on using the mouse to select the desired
tab is not the "fastest" way. My point is: given the standard behavior
of Ctrl-Tab (I agree that it is "... plain MDI window navigation."),
using the mouse may well be more efficient for some, depending on
circumstances. To each his own, eh?

Maybe I misunderstood it... sorry...

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
 
Hendrik said:
Carl Daniel [VC++ MVP]
Personally, I've never wanted the feature. My C++ coding style
rarely results in matched .cpp/.h pairs, so I don't miss the feature.

It certainly doesn't match all the time for
me -- but rarely ever? What do you do then?

value-types tend to be generic, hence entirely in the header with no
corresponding .cpp file.

application types use abstract interfaces, smart pointers and factory
patterns, so the full implementation type is typically declared entirely in
the .cpp file and there is no corresponding .h file.

-cd
 
Torben said:
Hi

Can anyone tell me what is the short-cut to switch between a .h and .cpp
file, thanks

Torben Laursen


I have one for Visual Studio .NET 2003 that I wrote (the API's changed
from 2002 so it will take some serious changes to work with the older
version). It needs to be imported into the Macro IDE and bound to a
hotkey or button.

It could probably be optimized to be faster but it does the job (esp if
the files are already open).

Imports EnvDTE
Imports System.Windows.Forms
Imports Microsoft.VisualStudio.VCProjectEngine
'Note: You may have to manually go into the 'References' of this macro project and add Microsoft.VisualStudio.VCProjectEngine to it manually

Public Module gbxUtilityMacros
Private Function SearchProjectForFiles(ByRef proj As VCProject, ByRef file1 As String, ByRef file2 As String, ByRef file3 As String, ByRef file4 As String) As Boolean
Dim fileColl As IVCCollection = proj.Files
Dim filecount As Integer = fileColl.Count
Dim counter As Integer
For counter = 1 To filecount
Dim file As VCFile = fileColl.Item(counter)
Dim origname As String = file.Name
Dim fullname As String = file.FullPath
If (String.Compare(origname, file1, True) = 0) Then
DTE.Documents.Open(fullname)
Return True
ElseIf (String.Compare(origname, file2, True) = 0) Then
DTE.Documents.Open(fullname)
Return True
ElseIf (String.Compare(origname, file3, True) = 0) Then
DTE.Documents.Open(fullname)
Return True
ElseIf (String.Compare(origname, file4, True) = 0) Then
DTE.Documents.Open(fullname)
Return True
End If
Next
Return False
End Function


Sub HeaderSourceSwitch()
On Error Resume Next

Dim sExtOffset As String = InStrRev(ActiveDocument.Name, ".")
Dim sExt As String
If sExtOffset > 0 Then
sExt = Right(ActiveDocument.Name, ActiveDocument.Name.Length - sExtOffset)
End If

sExt = LCase(sExt)
Dim sFileBase As String = Left(ActiveDocument.Name, Len(ActiveDocument.Name) - Len(sExt) - 1)

Dim PossibleMatch1 As String
Dim PossibleMatch2 As String
Dim PossibleMatch3 As String
Dim PossibleMatch4 As String

' valid extension?
If sExt = "h" Or sExt = "hpp" Or sExt = "hxx" Then
PossibleMatch1 = sFileBase & ".cpp"
PossibleMatch2 = sFileBase & ".cxx"
PossibleMatch3 = sFileBase & ".cc"
PossibleMatch4 = sFileBase & ".c"
ElseIf sExt = "c" Or sExt = "cpp" Or sExt = "cxx" Then
PossibleMatch1 = sFileBase & ".hpp"
PossibleMatch2 = sFileBase & ".hxx"
PossibleMatch3 = sFileBase & ".hh"
PossibleMatch4 = sFileBase & ".h"
Else
Exit Sub
End If

' is the needed file already opened?
Dim doc As Document
For Each doc In DTE.Documents
If (String.Compare(doc.Name, PossibleMatch1, True) = 0) Then
If doc.ActiveWindow() Is Nothing Then
DTE.Documents.Open(doc.FullName)
Else
doc.Activate()
End If
Exit Sub
End If
If (String.Compare(doc.Name, PossibleMatch2, True) = 0) Then
If doc.ActiveWindow() Is Nothing Then
DTE.Documents.Open(doc.FullName)
Else
doc.Activate()
End If
Exit Sub
End If
If (String.Compare(doc.Name, PossibleMatch3, True) = 0) Then
If doc.ActiveWindow() Is Nothing Then
DTE.Documents.Open(doc.FullName)
Else
doc.Activate()
End If
Exit Sub
End If
If (String.Compare(doc.Name, PossibleMatch4, True) = 0) Then
If doc.ActiveWindow() Is Nothing Then
DTE.Documents.Open(doc.FullName)
Else
doc.Activate()
End If
Exit Sub
End If
Next

Dim ThisProj As VCProject = DTE.ActiveDocument.ProjectItem.ContainingProject().Object
If SearchProjectForFiles(ThisProj, PossibleMatch1, PossibleMatch2, PossibleMatch3, PossibleMatch4) Then
Exit Sub
End If

Dim sol As Solution = DTE.Solution
Dim loopproj As Project
For Each loopproj In sol.Projects
Dim Proj As VCProject = loopproj.Object
'Minor optimization: Skip the project we already searched above
If Not Proj Is ThisProj Then
If SearchProjectForFiles(Proj, PossibleMatch1, PossibleMatch2, PossibleMatch3, PossibleMatch4) Then
Exit Sub
End If
End If
Next
End Sub
End Module
 
Hendrik said:
Carl Daniel said:
[...]

Thanks for posting this - I'm sure others will find it useful.

Yes, I would have, if Visual Assist wouldn't
have taken care of it already.
Personally, I've never wanted the feature. My C++ coding style rarely
results in matched .cpp/.h pairs, so I don't miss the feature.

It certainly doesn't match all the time for
me -- but rarely ever? What do you do then?
I do
remember this feature from way back in Turbo C/Borland C++ though.

Oh yeah, in BCB it was CTRL-F6 (as opposed
to CTRL-TAB which was normal MDI behaviour). :)

Actually -- Ctrl+F6 came first, and was the standard, and is still supported in
word processors (e.g. Word) where Ctrl+Tab is is used to insert a Tab character
in certain cases.
 
Back
Top