Click here to Skip to main content
Licence CPOL
First Posted 30 Apr 2002
Views 98,586
Downloads 790
Bookmarked 23 times

Switch between Header and CPP File for Visual Studio .NET

By , | 3 Jun 2008 | Article
Here are two methods that allow you to quickly switch between associated header and implementation files in Visual Studio .NET

Introduction

Here are two methods that allow you to quickly switch between the associated header and implementation files in Visual Studio .NET.

Pavel Sokolov's Solution

A simple macro that allows you to quickly switch between the associated header and implementation files. This is a modification for Visual Studio .NET of the macro by Nooruddin Kapasi.

This macro simply switches between a *.h file and a *.cpp file, without any errors (if the *.h or the *.cpp file does not exist).

The Code

Option Strict Off
Option Explicit Off

Imports EnvDTE
Imports Microsoft.VisualBasic

Public Module Switch

    Sub Switch()
        '////////////////////////////////////////////
        'Nooruddin Kapasi 1998.
        'Pavel Sokolov , CEZEO software , http://www.cezeo.com , Adaptation for .NET
        'DESCRIPTION: Switch Between Header and cpp
        '////////////////////////////////////////////

        Dim a As String
        Dim b As String
        Dim Flag As Integer
        Flag = 0
        a = DTE.ActiveDocument.FullName()
        tmp = InStr(a, ".cpp")
        If tmp Then
            b = Left(a, Len(a) - 3) + "h"
            Flag = 1
        Else
            tmp = InStr(a, ".h")
            If tmp Then
                b = Left(a, Len(a) - 1) + "cpp"
                Flag = 1
            End If
        End If

        If Flag Then
            DTE.Documents.Open(b, "Text")
        End If
    End Sub

End Module

Pierre Arnaud's Solution

The Switch_H_CPP macro switches between the active document (either source file or header file) and its counterpart (either header file or source file). Whereas simpler macros get this job done more efficiently (see the above), they do not work if the header files and source files are not stored in the same directory.

Switch_H_CPP looks for a matching counterpart by walking through all the loaded project files. It can, therefore, deduce the full path of the file and open it even if it is in a different directory as the active document.

Source

Here comes the source:

Option Strict Off
Option Explicit Off

Imports EnvDTE
Imports System.Diagnostics

Public Module Opac

    Function WalkProjectItemsAndOpen(ByRef items As ProjectItems, _
                                     ByRef name As String) As Boolean
        Dim item As ProjectItem

        If ReferenceEquals(items, Nothing) Then
            Return False
        End If

        For Each item In items
            Dim sub_items As ProjectItems
            sub_items = item.SubProject
            If Not sub_items Is Nothing Then
                If WalkProjectItemsAndOpen(sub_items, name) Then
                    Return True
                End If
            End If
            If item.Name = name Then
                'MsgBox("Found " + name + " in project " + _
                                   items.ContainingProject.UniqueName)
                Dim op As ItemOperations
                op = DTE.ItemOperations
                op.OpenFile(item.FileNames(0), Constants.vsViewKindTextView)
                Return True
            End If
        Next
        Return False
    End Function

    Sub Swap_H_CPP()
        Dim proj As Project
        Dim doc As Document
        Dim name As String

        doc = DTE.ActiveDocument
        name = doc.Name

        If name.EndsWith(".h") Then
            name = name.Replace(".h", ".cpp")
        ElseIf name.EndsWith(".cpp") Then
            name = name.Replace(".cpp", ".h")
        End If

        For Each proj In DTE.Solution.Projects
            If WalkProjectItemsAndOpen(proj.ProjectItems, name) Then
                Exit Sub
            End If
        Next

        Beep()

    End Sub

End Module

History

  • 20th September, 2002 - Updated Pierre Arnaud's solution to stop an exception being thrown in certain circumstances
  • 2nd June, 2008 - Fixed a typo in WalkProjectItemsAndOpen

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Authors

Pavel Sokolov

Web Developer

Russian Federation Russian Federation

Member

C++ programmer.

Pierre Arnaud

Web Developer

Switzerland Switzerland

Member

Pierre Arnaud got a Ph.D. in computer science at the Swiss Federal Institute of Technology; he currently works both as an independent contractor on hardware and software projects at OPaC bright ideas and as a senior software designer at EPSITEC.

Pierre was a key contributor to the Smaky computer, a real time, multitasking system based on the Motorola 680x0 processor family.

Now, Pierre works on his spare time for the Creative Docs .NET project: it is a vector based drawing and page layout software based on .NET and AGG.



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionTry this PinmvpDave Kerr23:05 3 Feb '12  
GeneralVisual Studio plugin that does this Pinmembereggplantriot20:01 8 Apr '10  
GeneralRe: Visual Studio plugin that does this PinmemberHoward Robinson4:58 15 Sep '10  
Generaltypo Pinmemberkamnas1:09 2 Jun '08  
AnswerVisual Studio 2005 Header Switch Macro [modified] PinmemberSnakefoot3:21 21 Jun '06  
GeneralRe: Visual Studio 2005 Header Switch Macro Pinmemberjcem4:24 24 Feb '07  
GeneralYet another adaptation... Pinmemberantoinecloutier14:39 27 Apr '06  
GeneralCumulative VC++ 2003 (ver 7.1.3088) CPP-H-Swapper PinmemberTedimDim1:50 1 Apr '06  
Option Strict Off
Option Explicit Off
 
Imports EnvDTE
Imports System.Diagnostics
 
Public Module Opac
 
Function WalkProjectItemsAndOpen(ByRef items As ProjectItems, ByRef name As String) As Boolean
Dim item As ProjectItem
Dim item_name As String
 
If ReferenceEquals(items, Nothing) Then
Return False
End If
 
For Each item In items
Dim sub_items As ProjectItems
sub_items = item.ProjectItems
If Not sub_items Is Nothing Then
If WalkProjectItemsAndOpen(sub_items, name) Then
Return True
End If
End If
item_name = item.FileNames(1).ToLower
If item_name = name Then
' MsgBox("Found " + name + " in project " + items.ContainingProject.UniqueName)
Dim op As ItemOperations
op = DTE.ItemOperations
op.OpenFile(item.FileNames(0), Constants.vsViewKindTextView)
Return True
End If
Next
Return False
End Function
 
Sub Swap_H_CPP()
Dim proj As Project
Dim doc As Document
Dim name As String
 
doc = DTE.ActiveDocument
name = doc.Path & doc.Name
name = name.ToLower
 
' First try to switch from '.h' to '.cpp' then vice versa
If name.EndsWith(".h") Then
name = name.Replace(".h", ".cpp")
ElseIf name.EndsWith(".hpp") Then
name = name.Replace(".hpp", ".cpp")
ElseIf name.EndsWith(".cpp") Then
name = name.Replace(".cpp", ".h")
ElseIf name.EndsWith(".c") Then
name = name.Replace(".c", ".h")
 
ElseIf name.EndsWith(".h") Then
name = name.Replace(".h", ".c")
ElseIf name.EndsWith(".hpp") Then
name = name.Replace(".hpp", ".c")
ElseIf name.EndsWith(".cpp") Then
name = name.Replace(".cpp", ".hpp")
ElseIf name.EndsWith(".c") Then
name = name.Replace(".c", ".hpp")
End If
 
For Each proj In DTE.Solution.Projects
If WalkProjectItemsAndOpen(proj.ProjectItems, name) Then
Exit Sub
End If
Next
 
Beep()
 
End Sub
 
End Module

GeneralRe: Cumulative VC++ 2003 (ver 7.1.3088) CPP-H-Swapper PinmemberPierre Arnaud19:52 1 Apr '06  
GeneralHOW Pinmemberalan935:12 7 Apr '05  
GeneralFolder problem PinmemberMr Scotty4:17 5 Jan '05  
GeneralExcellent PinsussAnonymous23:38 27 Nov '03  
QuestionHow to install? PinsussAnonymous1:18 7 Nov '03  
QuestionHow to install? PinsussAnonymous1:18 7 Nov '03  
AnswerI have the same problem in VS .NET 2003 PinmemberSkyhawk12:13 13 Jan '04  
GeneralRe: I have the same problem in VS .NET 2003 PinmemberPavel Sokolov21:28 22 Jan '04  
GeneralRe: I have the same problem in VS .NET 2003 PinmemberShroombaker12:11 19 Apr '04  
GeneralRe: I have the same problem in VS .NET 2003 PinsussRehan A7:36 30 Apr '04  
AnswerRe: How to install? PinmemberAlan Balkany7:22 17 Mar '10  
GeneralFix for filename case sensitivity Pinmemberhgohel6:38 21 Sep '03  
GeneralFix for VS .NET 2003 Pinmemberdr00g23:43 7 May '03  
GeneralRe: Fix for VS .NET 2003 PinmemberTodd C. Gleason7:00 28 Aug '03  
GeneralThis doesn't work in Final Beta 2003 PinmemberNevering12:58 22 Mar '03  
GeneralRe: This doesn't work in Final Beta 2003 PinmemberNevering13:11 22 Mar '03  
GeneralRe: This doesn't work in Final Beta 2003 Pinmembersakamaki2:40 18 Jul '06  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120529.1 | Last Updated 3 Jun 2008
Article Copyright 2002 by Pavel Sokolov, Pierre Arnaud
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid