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.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.