Chapter 13: Extending Visual Studio 2010

389

Within the CommandBars collection, menuBarCommandBar, you then look into the Controls collection, which is a list of menus on the main menu to find the Tools menu, assigned to toolsControl as follows:

C#:

string toolsMenuName = "Tools"; CommandBarControl toolsControl =

menuBarCommandBar.Controls[toolsMenuName];

VB:

Dim toolsMenuName As String = "Tools" Dim toolsControl As CommandBarControl =

menuBarCommandBar.Controls.Item(toolsMenuName)

In the VS automation object model, an individual menu is a CommandBarPopup, assigned to toolsPopup as follows:

C#:

CommandBarPopup toolsPopup = (CommandBarPopup)toolsControl;

VB:

Dim toolsPopup As CommandBarPopup =

CType(toolsControl, CommandBarPopup)

Now you have a reference to the menu where the menu item for the Add-In must be added. You are ready to add the command, using the AddNamedCommand2 method of the commands collection. Remember that earlier code assigned these commands from the application object to the commands variable. A quick review of the arguments to AddNamedCommand2 gives you the gist of what’s happening: The code passes a reference to the Add-In; provides a menu item name and description; and indicates that the status of the command is supported and enabled, the menu item will have pictures and text, and the type of menu item is button (can be clicked). If you want all the details of this method call, now is a good time to refer to the documentation. While it’s important to understand the major interfaces, such as OnConnection for IDTExtensibility2, memorizing every API call might not be the most productive use of your time when you’re just starting out. The following code shows the call to AddNamedCommand2:

C#:

Command command = commands.AddNamedCommand2( _addInInstance, "KeystrokeFinder", "KeystrokeFinder",

"Executes the command for KeystrokeFinder",

Page 412
Image 412
Microsoft 9GD00001 manual 389