Chapter 13: Extending Visual Studio 2010

391

As you may recall, the OnConnection method assigned the main application object to _applicationObject, a field of the Connect class. This is important because now you have access to the main application object, and you’ll see how it’s used in the next section, which shows you how to execute your Add-In via the Exec method.

Implementing the Exec Method

Whenever a user starts your Add-In, VS calls the Exec method of the IDTCommandTarget interface. The Exec method is important because that’s where you add your code to implement the behavior of your Add-In. The previous sections discussed code that is generated by VS, but Listing 13-3 contains code for the Exec method that you should enter yourself to make the KeystrokeFinder Add-In work. The purpose of the Add-In

for this section is to list all VS commands and their associated shortcut keys. The list of commands and shortcuts will be displayed in the VS Output window. Listing 13-3 shows the Exec method for the KeystrokeFinder Add-In.

Listing 13-3 Implementing the Exec method

C#:

public void Exec(

string commandName, vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled)

{

handled = false; if(executeOption ==

vsCommandExecOption.vsCommandExecOptionDoDefault)

{

if (commandName == "KeystrokeFinder.Connect.KeystrokeFinder")

{

OutputWindow outWin = _applicationObject.ToolWindows.OutputWindow;

OutputWindowPane outPane = outWin.OutputWindowPanes.Add(

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

foreach (Command cmd in _applicationObject.Commands)

{

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

Page 414
Image 414
Microsoft 9GD00001 manual Implementing the Exec Method, 391, Listing 13-3 Implementing the Exec method