394Microsoft Visual Studio 2010: A Beginner’s Guide

As you learned earlier, the application object is the starting point for accessing all VS objects. Since we need to write to the Output window, the code accesses the ToolWindows property of the application object, which provides access to multiple VS windows. The following code obtains a reference to the OutputWindow, adds a new pane, and activates the pane:

C#:

OutputWindow outWin = _applicationObject.ToolWindows.OutputWindow;

OutputWindowPane outPane = outWin.OutputWindowPanes.Add(

"Keyboard Shortcuts"); outPane.Activate();

VB:

Dim outWin As OutputWindow = _applicationObject.ToolWindows.OutputWindow

Dim outPane As OutputWindowPane = outWin.OutputWindowPanes.Add(

"Keyboard Shortcuts")

outPane.Activate()

Going back to the application object, we need to access the Commands collection, using a foreach loop to access each Command object. Each command name is in the Name property. The Bindings property is a collection of shortcut keys for the command. Some commands have no shortcut keys, as indicated by an empty Bindings collection (its Length property will be set to 0), so we skip them. The following code shows how to iterate through all VS commands and print each command name and associated shortcut keys to the Output window:

C#:

foreach (Command cmd in _applicationObject.Commands)

{

object[] cmdBindings = cmd.Bindings as object[];

if (cmdBindings.Length > 0)

{

string bindingStr = string.Join(", ", cmdBindings);

outPane.OutputString( "Command: " + cmd.Name +

Page 417
Image 417
Microsoft 9GD00001 manual Microsoft Visual Studio 2010 a Beginner’s Guide