Click here to Skip to main content
15,867,308 members
Articles / Programming Languages / C++

Switch between Header and CPP File for Visual Studio .NET

,
Rate me:
Please Sign up or sign in to vote.
4.71/5 (11 votes)
3 Jun 2008CPOL1 min read 163.4K   1K   23   33
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

VB.NET
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:

VB.NET
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)


Written By
Web Developer
Switzerland Switzerland

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.


Written By
Web Developer
Russian Federation Russian Federation
C++ programmer.

Comments and Discussions

 
AnswerBuilt-In Feature in VS2012 Pin
WhatIsHeDoing1-Aug-13 6:04
WhatIsHeDoing1-Aug-13 6:04 
QuestionTry this Pin
Dave Kerr3-Feb-12 23:05
mentorDave Kerr3-Feb-12 23:05 
GeneralVisual Studio plugin that does this Pin
eggplantriot8-Apr-10 20:01
eggplantriot8-Apr-10 20:01 
GeneralRe: Visual Studio plugin that does this Pin
Howard Robinson15-Sep-10 4:58
Howard Robinson15-Sep-10 4:58 
Generaltypo Pin
kamnas2-Jun-08 1:09
kamnas2-Jun-08 1:09 
AnswerVisual Studio 2005 Header Switch Macro [modified] Pin
Rolf Kristensen21-Jun-06 3:21
Rolf Kristensen21-Jun-06 3:21 
GeneralRe: Visual Studio 2005 Header Switch Macro Pin
jcem24-Feb-07 4:24
jcem24-Feb-07 4:24 
GeneralYet another adaptation... Pin
antoinecloutier27-Apr-06 14:39
antoinecloutier27-Apr-06 14:39 
GeneralCumulative VC++ 2003 (ver 7.1.3088) CPP-H-Swapper Pin
TedimDim1-Apr-06 1:50
TedimDim1-Apr-06 1:50 
GeneralRe: Cumulative VC++ 2003 (ver 7.1.3088) CPP-H-Swapper Pin
Pierre Arnaud1-Apr-06 19:52
Pierre Arnaud1-Apr-06 19:52 
GeneralHOW Pin
alan937-Apr-05 5:12
alan937-Apr-05 5:12 
GeneralFolder problem Pin
Mr Scotty5-Jan-05 4:17
Mr Scotty5-Jan-05 4:17 
GeneralExcellent Pin
Anonymous27-Nov-03 23:38
Anonymous27-Nov-03 23:38 
QuestionHow to install? Pin
Anonymous7-Nov-03 1:18
Anonymous7-Nov-03 1:18 
QuestionHow to install? Pin
Anonymous7-Nov-03 1:18
Anonymous7-Nov-03 1:18 
AnswerI have the same problem in VS .NET 2003 Pin
Skyhawk13-Jan-04 12:13
Skyhawk13-Jan-04 12:13 
GeneralRe: I have the same problem in VS .NET 2003 Pin
Pavel Sokolov22-Jan-04 21:28
Pavel Sokolov22-Jan-04 21:28 
GeneralRe: I have the same problem in VS .NET 2003 Pin
Shroombaker19-Apr-04 12:11
Shroombaker19-Apr-04 12:11 
GeneralRe: I have the same problem in VS .NET 2003 Pin
Rehan A30-Apr-04 7:36
sussRehan A30-Apr-04 7:36 
AnswerRe: How to install? Pin
Alan Balkany17-Mar-10 7:22
Alan Balkany17-Mar-10 7:22 
GeneralFix for filename case sensitivity Pin
H. Gohel21-Sep-03 6:38
H. Gohel21-Sep-03 6:38 
Here's a simple fix for files that have a mix of .H, .CPP, .Cpp or any other styles for which Pierre's code won't work off the bat.

Simply change these two lines:

In Swap_H_CPP() change
name = doc.Name
to
name = doc.Name.ToLower

and also in Function WalkProjectItemsAndOpen() change
If item.Name = name Then
to
If item.Name.ToLower = name Then

Now it should work in more cases.

GeneralFix for VS .NET 2003 Pin
macwkize7-May-03 23:43
macwkize7-May-03 23:43 
GeneralRe: Fix for VS .NET 2003 Pin
Todd C. Gleason28-Aug-03 7:00
Todd C. Gleason28-Aug-03 7:00 
GeneralThis doesn't work in Final Beta 2003 Pin
Nevering22-Mar-03 12:58
Nevering22-Mar-03 12:58 
GeneralRe: This doesn't work in Final Beta 2003 Pin
Nevering22-Mar-03 13:11
Nevering22-Mar-03 13:11 

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

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