Skip to main content
Email Password   helpLost your password?

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

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
Generaltypo Pin
kamnas
2:09 2 Jun '08  
AnswerVisual Studio 2005 Header Switch Macro [modified] Pin
Snakefoot
4:21 21 Jun '06  
GeneralRe: Visual Studio 2005 Header Switch Macro Pin
jcem
5:24 24 Feb '07  
GeneralYet another adaptation... Pin
antoinecloutier
15:39 27 Apr '06  
GeneralCumulative VC++ 2003 (ver 7.1.3088) CPP-H-Swapper Pin
TedimDim
2:50 1 Apr '06  
GeneralRe: Cumulative VC++ 2003 (ver 7.1.3088) CPP-H-Swapper Pin
Pierre Arnaud
20:52 1 Apr '06  
GeneralHOW Pin
alan93
6:12 7 Apr '05  
GeneralFolder problem Pin
Mr Scotty
5:17 5 Jan '05  
GeneralExcellent Pin
Anonymous
0:38 28 Nov '03  
GeneralHow to install? Pin
Anonymous
2:18 7 Nov '03  
GeneralHow to install? Pin
Anonymous
2:18 7 Nov '03  
GeneralI have the same problem in VS .NET 2003 Pin
Skyhawk
13:13 13 Jan '04  
GeneralRe: I have the same problem in VS .NET 2003 Pin
Pavel Sokolov
22:28 22 Jan '04  
GeneralRe: I have the same problem in VS .NET 2003 Pin
Shroombaker
13:11 19 Apr '04  
GeneralRe: I have the same problem in VS .NET 2003 Pin
Rehan A
8:36 30 Apr '04  
GeneralFix for filename case sensitivity Pin
hgohel
7:38 21 Sep '03  
GeneralFix for VS .NET 2003 Pin
dr00g
0:43 8 May '03  
GeneralRe: Fix for VS .NET 2003 Pin
Todd C. Gleason
8:00 28 Aug '03  
GeneralThis doesn't work in Final Beta 2003 Pin
Nevering
13:58 22 Mar '03  
GeneralRe: This doesn't work in Final Beta 2003 Pin
Nevering
14:11 22 Mar '03  
GeneralRe: This doesn't work in Final Beta 2003 Pin
sakamaki
3:40 18 Jul '06  
GeneralError when using Swap_H_CPP Pin
hector031
10:36 9 Jan '03  
GeneralRe: Error when using Swap_H_CPP Pin
Pierre Arnaud
20:41 9 Jan '03  
GeneralProblem defining 'proj As Project' Pin
Anonymous
12:44 11 Sep '02  
GeneralRe: Problem defining 'proj As Project' Pin
Pierre Arnaud
11:49 12 Sep '02  


Last Updated 3 Jun 2008 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2009