Click here to Skip to main content
General Programming » Macros and Add-ins » VS.NET Add-ins     Intermediate License: The Code Project Open License (CPOL)

Visual Studio .NET Shortcuts for Toggling Tool Windows

By Kevin McFarlane

Activate common tool windows without using the mouse
C++, Windows, Visual-Studio, Dev
Revision:2 (See All)
Posted:14 May 2002
Views:47,062
Bookmarked:13 times
7 votes for this article.
Popularity: 3.73 Rating: 4.42 out of 5

1

2

3
2 votes, 33.3%
4
4 votes, 66.7%
5

Introduction

When working in the Developer Studio IDE for Visual C++ 4, 5 and 6 I got used to the convenience of being able to toggle the Workspace and Output windows between "open" and "closed" states with the click of a button. The Workspace window in Developer Studio contained, as window tabs, File View, Class View and Resource View. These are windows in their own right in the Visual Studio .NET IDE. (Solution Explorer is the equivalent of Developer Studio's File View.) In Developer Studio the buttons for toggling the Workspace and Output windows were available on the toolbar. I also used to assign the commands to keyboard shortcuts, e.g.,

Alt+X,W = View.ToggleWorkspaceWindow
Alt+X,O = View.ToggleOutputWindow

I found this more convenient than having to use the menu and the mouse. 

In the new Microsoft Development Environment for Visual Studio .NET, which contains a lot more windows (called Tool Windows), it would be nice to be able to do something similar. I find that for C# and Visual Basic I frequently access the following Tool Windows:

  • Solution Explorer
  • Class View
  • Toolbox
  • Properties
  • Macro Explorer

Often I'll want to access a window and perform an operation, such as set a property or select a control or navigate to a method, and then write some code. When I'm writing code I will often want access to the full width of the screen. The Auto Hide feature of the IDE is great for this. However, sometimes I want to be able to activate a particular tool window without using the mouse and I may want to disable Auto Hide for a while and then close the tool window again without using the mouse. You can do this with a combination of macros and command aliases.

Activating a Tool Window

To do this using the keyboard you can of course use the "Alt" keyboard shortcuts for accessing the menu commands or memorise a predefined assigned keyboard shortcut if one exists. For example, to view Solution Explorer:

Alt+V, P or Ctrl+Alt+L

However, these aren't, in general, easy to remember. This is where command aliases come in handy. See the topic, Creating Custom Aliases for Visual Studio Commands, in the Visual Studio online help. A command alias is a custom name for an IDE command.

Here's how to define a command alias.

Open the command window (View->Other Windows->Command Window).

At the > prompt type alias, then the name for the alias, followed by the command you want to create an alias for. For example:

alias vs View.SolutionExplorer

If you now type vs at the > prompt the IDE command, View.SolutionExplorer, is executed and the Solution Explorer window will appear activated. 

This can be made more convenient because command aliases can also be created and executed in the Find text box on the toolbar (referred to as the Find/Command box in the online help). If you type > in the Find/Command box it switches to command mode. For example, if you enter >vs the above alias will be executed. You can also explicitly enter >View.SolutionExplorer or any other IDE command and it will be executed. Intellisense auto-completion is also available. 

As an added convenience you can quickly enter command mode by typing Ctrl + /. So the quickest way to execute the alias we've defined for activating the Solution Explorer window is:

Ctrl + / followed by vs.

Hiding (Closing) a Tool Window

This is a bit more elaborate. There is probably a better way of doing this but this is the method I've adopted, which is good enough for now. First, activate the window:

View.SolutionExplorer

Then close it:

Window.CloseToolWindow

As far as I can see you can't combine these into a single alias (but please correct me if I'm wrong) so, instead, we can define a VB macro that combines them. Then we can define an alias that executes the macro.

Here is the macro

Sub CloseSolutionExplorer()
  DTE.ExecuteCommand("View.SolutionExplorer")
  DTE.ExecuteCommand("Window.CloseToolWindow")
End Sub

To create a Macro, select View->Other Windows->Macro Explorer. Select, say, the MyMacros project and then add a new module. Then write a Sub procedure as above.

An alias would look like this:    

alias hs Macros.MyMacros.General.CloseSolutionExplorer

where MyMacros is the project name and General is the module name.

So to close Solution Explorer type:

Ctrl + / followed by hs

I have repeated this exercise for the Class View, Properties, Tool Box and Macro Explorer windows and the results are shown below.

Summary of Shortcuts

Command Aliases

alias vc View.ClassView
alias vm View.MacroExplorer
alias vp View.PropertiesWindow
alias vs View.SolutionExplorer
alias vt View.Toolbox
alias hc Macros.MyMacros.General.CloseClassView
alias hm Macros.MyMacros.General.CloseMacroExplorer
alias hp Macros.MyMacros.General.ClosePropertiesWindow
alias hs Macros.MyMacros.General.CloseSolutionExplorer
alias ht Macros.MyMacros.General.CloseToolbox

Macros

Sub CloseClassView()
  DTE.ExecuteCommand("View.ClassView")
  DTE.ExecuteCommand("Window.CloseToolWindow")
End Sub

Sub CloseToolbox()
  DTE.ExecuteCommand("View.Toolbox")
  DTE.ExecuteCommand("Window.CloseToolWindow")
End Sub

Sub CloseMacroExplorer()
  DTE.ExecuteCommand("View.MacroExplorer")
  DTE.ExecuteCommand("Window.CloseToolWindow")
End Sub

Sub ClosePropertiesWindow()
  DTE.ExecuteCommand("View.PropertiesWindow")
  DTE.ExecuteCommand("Window.CloseToolWindow")
End Sub

Sub CloseSolutionExplorer()
  DTE.ExecuteCommand("View.SolutionExplorer")
  DTE.ExecuteCommand("Window.CloseToolWindow")
End Sub

Note: The "Hide (Close) Window" aliases should not be called if the window is in Auto Hide mode, otherwise you will get an error message saying Command "Window.CloseToolWindow" is not available. This is innocuous though. If you want to suppress the error message you can modify the macro along the following lines:

Sub CloseSolutionExplorer()
  Try
    DTE.ExecuteCommand("View.SolutionExplorer")
    DTE.ExecuteCommand("Window.CloseToolWindow")
  Catch ex As System.Runtime.InteropServices.COMException
  Catch ex As System.Exception
    MsgBox(ex.GetType().ToString() & vbNewLine & vbNewLine & ex.Message)
  End Try
End Sub

The error that the macro generates is a System.Runtime.InteropServices.COMException. So, catching it and doing nothing will suppress the dialog.

Outstanding Issues

Ideally, it would be nice if we could have a single command that operates as a toggle rather than having to have paired aliases, such as vs and hs. However, I have not been able to figure out how to do this in the Visual Studio Automation model.

License

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

About the Author

Kevin McFarlane


Member

Occupation: Web Developer
Location: United Kingdom United Kingdom

 Msgs 1 to 7 of 7 (Total in Forum: 7) (Refresh)FirstPrevNext
GeneralSimilar Code to Switch Between Multiple LayoutsmemberKRowe9:33 3 Oct '09  
I like to setup VS so that I can assign a button which will switch between tool layouts. Here is the code I have:

    ' Macro: CycleLayouts()
' This macro will switch between preset window layouts.
' Layouts are grouped for (d)esign and (c)ode layouts.
Public Sub CycleLayouts()
Dim c(0 To 1) As Window, d(0 To 1) As Window
c(0) = DTE.Windows.Item(Constants.vsWindowKindSolutionExplorer)
c(1) = DTE.Windows.Item(Constants.vsWindowKindMacroExplorer)
d(0) = DTE.Windows.Item(Constants.vsWindowKindProperties)
d(1) = DTE.Windows.Item(Constants.vsWindowKindToolbox)

If d(0).Visible Then ' Hide All
DisplayLayout(c, False)
DisplayLayout(d, False)
ElseIf c(0).Visible Then ' Designer Layout
DisplayLayout(d)
Else ' Coder Layout
DisplayLayout(c)
c(1).AutoHides = True ' Autohide macro window
End If
End Sub

' Macro: DisplayLayout()
' This macro will display or hide all windows in the array that it is passed.
Private Sub DisplayLayout(ByRef w() As Window, Optional ByVal bShow As Boolean = True)
For Each wnd As Window In w
wnd.Visible = bShow
Next
End Sub


I have tried to make it as easy as possible to modify for your needs.
GeneralHow to open and close a Tool Window in VS 2005 (and beyond)memberSuresh R Iyer3:39 13 Jun '09  
One of the frustrations I have been having with VS 2005 has been the inability to access the "Auto Hide" toggle button (that is available in each of the tool windows) via the keyboard. I wanted a way to pin and unpin the tool windows using keyboard, as well as have a provision to hide the tool window.

I had read this post earlier but wasn't sure whether I wanted to solve the problem this way. Plus, it was not too convenient a way as well. Of course, this post was written in 2002-03 and so the earlier version of VS might not have been as powerful as VS 2005.

Today, I was catching up on the power of the "Find/Command" box[^], and I had, a few weeks back, learned how to create keyboard shortcuts[^] for those features for which there is no default keyboard shortcut.

While googling, I saw this post again today, it got me thinking about whether there being a possibility of having commands to toggle the tool windows, and sure, there they were: "Window.AutoHide" and "Window.AutoHideAll".

Window.AutoHide will hide the tool window having focus
Window.AutoHideAll will hide all the tool windows that are open

I think the easiest way to do solve this problem is to assign keyboard shortcuts to those two commands. So, here is what I have done:

Assign "Ctrl + ~" to Window.AutoHide and assign "Ctrl + Shift + ~" to Window.AutoHideAll.

What this does is allow me to toggle auto hide for the window having focus using "Ctrl + ~", and to hide all the tool windows (that might or might not be pinned) using "Ctrl + Shift + ~". I am happy I have finally cracked the problem. It would have been far more useful if Microsoft solves this for everyone (by providing default shortcuts for these two commands).

Finally, as @gkuehn mentioned, you can use "Shift + Esc" to hide a tool window that has focus.

Hope some keyboard junkie like me will find this message useful. Thanks.

Suresh R Iyer
http://twitter.com/protoiyer
GeneralExpanded Toggle Macromemberkonmel14:49 8 Apr '09  
Kevin, I expanded your macro a little further. Now it will Toggle the window if the focus is on it. And it will bring focus to it if the focus somewhere else in VS

Sub ToggleWindow(ByVal windowObjectKind As String)

Dim windowToToggle As Window = DTE.Windows.Item(windowObjectKind)
Dim activeWindow As Window = DTE.ActiveWindow

If windowToToggle.Visible Then
If activeWindow.ObjectKind <> windowObjectKind Then
windowToToggle.Activate()
Else
windowToToggle.Close()
Try
DTE.ExecuteCommand("Window.ActivateDocumentWindow")
Catch ex As Exception

End Try
End If
Else
windowToToggle.Visible = True
windowToToggle.Activate()
End If

End Sub


Now you can use it like this:

Sub ToggleSolutionExplorer()
ToggleWindow(Constants.vsWindowKindSolutionExplorer)
End Sub

Public Sub ToggleCommandLine()
ToggleWindow(Constants.vsWindowKindCommandWindow)
End Sub
GeneralHow to open and close a Tool Windowmembergkuehn22:32 27 Sep '07  
Hey,

I don't know if this hasn't worked on older VS versions, but to open the Solution Explorer you can press

CTRL - ALT - L

and to close a Tool Window in general press

SHIFT - ESC


Best,
G.
GeneralAutoHide makes it even bettermemberOdenni8:19 26 Jan '04  
Try adding the Window.AutoHide command to the mix to make the windows slide out from the side and slide back in when you're done. Window.AutoHideAll is also a convenient command to map to a shortcut key.
GeneralRe: AutoHide makes it even bettermemberKevin McFarlane3:18 27 Jan '04  
Cheers, I'll try that when I next get back into VS .NET mode. I'm fiddling around with 16-bit C++ at the moment!Cry

Kevin
GeneralThe proper way to togglememberKevin McFarlane4:30 31 Jul '03  
Girish Srinivasan just sent me the following solution to my "Outstanding Issues" section.


Sub ToggleSolutionExplorer()
Dim w1 As Window = DTE.Windows.Item(Constants.vsWindowKindSolutionExplorer)

If w1.Visible Then
w1.Visible = False
Else
w1.Visible = True
End If

End Sub


Kevin

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 14 May 2002
Editor: Sean Ewington
Copyright 2002 by Kevin McFarlane
Everything else Copyright © CodeProject, 1999-2010
Web20 | Advertise on the Code Project