Command Objects

Every action that is possible to execute through the menus and toolbars in Visual Studio is generically referred to as a command. For example, pasting text into a window is a command, as is building a project, toggling a breakpoint, and closing a window.

For each command supported in the IDE, there is a corresponding Command object; the DTE.Commands collection holds all the valid Command object instances. Each command is keyed by a name that categorizes, describes, and uniquely identifies the command. The Paste command, for instance, is available via the string key "Edit.Paste". If you want to retrieve the Command object mapping to the Paste command, you pull from the Commands collection using that string key.

Commands2 commands = (Commands2)_applicationObject.Commands;
Command cmd = commands.Item["Edit.Paste"];

You can query a command’s name via its Name property.

//name would = "Edit.Paste"
string name = cmd.Name;

Table 14.14 contains the members declared on the Command interface.

Image

TABLE 14.14 Command Members

The list of all available commands is extremely long (nearly 3,000 total), so it is impossible to cover every one of them, or even a large portion of them, here. To get an idea of the specific commands available, however, you can use the dialog box used that customizes the Visual Studio toolbars. If you select the Customize option from the View, Toolbars menu and then click the Commands tab, you can investigate all the various commands by category (see Figure 14.8). Another alternative is to programmatically iterate the DTE.Commands collection and view them that way.

Image

FIGURE 14.8 Using the Customize dialog box to view commands.

So, although we can’t cover all the commands, you can learn how to perform common tasks with the Command objects, such as executing a command, checking on a command’s current status, and even adding your own commands to the command library.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset