Contents
Visual Studio .NET (VS.NET) is a great development environment. However, no development environment is perfect. I always find things that are missing or could be improved. For instance, when programming in ASP.NET, I am always switching between the .aspx or .ascx and the associated Code Behind file. I found that there is no feature to toggle between these two files, however, all was not lost because the VS.NET team knew that they could not provide features for all users, so they opened up the VS.NET Automation Object Model for us to extend the development environment. First, I searched the Internet to find out if another user had come across problems and had found a solution. I did not find a solution to my problem, but I did find a macro [^] to switch between .cpp (C++) files and .h (C++ header) files. I ended up writing my own macro to switch between ASP.NET files and code behind files.
Sub ToggleAspNetCodeBehindFile()
Dim langExtension() As String = {".cs", ".vb", ".jsl"}
Dim activeDoc As String
Try
activeDoc = LCase(ActiveDocument().FullName)
If (InStr(activeDoc, ".aspx") Or _
InStr(activeDoc, ".ascx") Or _
InStr(activeDoc, ".asmx") Or _
InStr(activeDoc, ".asax")) Then
Dim i As Integer
Dim isCodeBehind As Boolean = False
Dim extensionLength As Integer = 0
For i = 0 To langExtension.Length - 1
If activeDoc.EndsWith(langExtension(i)) Then
isCodeBehind = True
extensionLength = langExtension(i).Length
Exit For
End If
Next
If (isCodeBehind) Then
OpenAspNetFile(activeDoc, extensionLength)
Else
OpenCodeBehindFile(activeDoc)
End If
Exit Sub
End If
Catch
MsgBox("Please select an ASP.NET document to toggle.", _
MsgBoxStyle.OKOnly, "No ASP.NET Document Selected")
End Try
End Sub
Private Sub OpenAspNetFile(ByVal activeDoc As String, _
ByVal extensionLength As Integer)
Dim fileName As String
Dim projItem As ProjectItem
If (InStr(activeDoc, ".asax") Or InStr(activeDoc, ".asmx")) Then
If (InStr(DTE.ActiveWindow.Caption, "design", _
CompareMethod.Text)) Then
ActiveDocument.Close(vsSaveChanges.vsSaveChangesPrompt)
End If
projItem = DTE.Solution.FindProjectItem(activeDoc)
projItem.Open(Constants.vsViewKindCode).Activate()
Else
fileName = Left(activeDoc, activeDoc.Length - extensionLength)
Try
projItem = DTE.Solution.FindProjectItem(fileName)
projItem.Open(Constants.vsViewKindTextView).Activate()
Catch
MsgBox(ActiveDocument().Name & " is not a valid " _
& "Code-Behind file.", MsgBoxStyle.OKOnly, _
"ASP.NET file not found")
End Try
End If
End Sub
Private Sub OpenCodeBehindFile(ByVal activeDoc As String)
Dim projItem As ProjectItem
projItem = DTE.Solution.FindProjectItem(activeDoc)
projItem.Open(Constants.vsViewKindCode)
End Sub
Before you can use the above macro, it must be installed.
Installation is simple. Copy the code above into a module in the Macro IDE and then save the file. You can also use the pre-compiled version available in the download file. Choose "Load Macro Project" from the "Tools/Macros" menu. Open the file AspNetMacros.vsmacros extracted from the downloaded archive file.
Now you can toggle between ASP.NET and code behind files using the Macro Explorer.
This method to use the macro is not very efficient. It is a good thing that Microsoft allows us to assign "Shortcut Keys" to macros.
To assign a shortcut key, click on the Tools/Options menu choice. In the dialog, open the Environment folder, click on Keyboard. Next, search for the macro in the "Show commands containing:" text box by typing toggleasp. You should then see the macro listed in the big text box below the search box. Click on the ToggleAspNetCodeBehindFile macro. If more than one is listed, make sure "Use new shortcut in:" is set to "Global." Add your shortcut key to the "Press shortcut key(s):" text box. I currently have mine assigned to "Alt-Space."
Make sure the shortcut you enter is not currently assigned, by checking the "Shortcut currently used by:" drop down box. If it is, I would choose another shortcut key. Click "OK" to assign the shortcut key. VS.NET will warn you about not being able to add the shortcut key to the default keyboard mapping scheme if you have not changed the keyboard mappings. If this happens, VS.NET will offer to make a copy. When it does, click "OK" and your shortcut key will be assigned.
To test the shortcut key, open an ASP.NET file and press your shortcut key. The code behind file should open. Press it again and the focus should be set back to the ASP.NET file. It does not matter which file you have opened, the macro will open the associated file.
I hope that you find this macro useful and would not need to uninstall it. However, uninstallation of the macro is easy. If you copied and pasted the code into a macro module, delete the pasted code. If you loaded the macro project, open the macro explorer, click on the macro project to remove. Then, in the Tools/Macros menu, click "Unload Macro Project". If you assigned a shortcut key, it will be removed automatically.
TypeFinder is a macro that will search for the namespace the type belongs to and insert it before the type or optionally add the Import
(VB.NET) or using
(C#) statements at the top of your code. The link to TypeFinder [^] did not work the last time I checked, but hopefully it will be working soon.
CollapseAll is a simple macro to collapse all the project nodes in the Solution Explorer. Click here for CollapseAll[^]
This is a small macro I wrote to add a feature to VS.NET that Andres Aguiar [^] thought should have been included.
Sub CloseAllOtherTabs()
Try
Dim currentDocument As String = DTE.ActiveDocument.FullName
DTE.Documents.CloseAll()
DTE.ItemOperations.OpenFile(currentDocument)
Catch
MsgBox("Please make sure you have selected a Tab before running the " _
& "CloseAllOtherTabs macro.", _
MsgBoxStyle.OKOnly, "CloseAllOtherTabs")
End Try
End Sub
This macro has filled a gap for me in VS.NET and made me more productive. I hope this macro helps you to be more productive too.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.