Recording and Using Macros in Visual Studio 2010

Visual Studio macros are part of the environment and are available to any language. Macro options are accessible from the Tools O Macros menu, as shown in Figure 1-38. The concept of macros is simple: Record a series of keystrokes and/or menu actions, and then play them back by pressing a certain keystroke combination.

FIGURE 1-38

For example, suppose that one particular function call with a complex set of arguments is constantly being called on in code, and the function call usually looks the same except for minor variations in the arguments. The keystrokes to code the function call could be recorded and played back as necessary, which would insert code to call the function, which could then be modified as necessary.

Macros can be far more complex than this, containing logic as well as keystrokes. The macro capabilities of Visual Studio are so comprehensive that macros have their own IDE (accessed via Tools O Macros O Macros IDE).

Macros can also be developed from scratch in this environment, but more commonly they are recorded using the Record Temporary Macro option on the Macros menu and then renamed and modified in the development environment. Here is an example of recording and modifying a macro:

1. Start a new Windows Application project.

2. In the new project, add a button to Form1, which was created with the project.

3. Double-click the button to get to its Click event routine.

4. Select Tools O Macros O Record Temporary Macro. A small toolbar (see Figure 1-39) will appear on top of the IDE with buttons to control the recording of a macro (Pause, Stop, and Cancel).

5. Press Enter and then type the following line of code:

TextBoxl.Text = "Macro Test"

6. Press Enter again.

7. In the small toolbar, press the Stop button.

8. Select Tools O Macros O Macro Explorer. The Macro Explorer will appear (in the location normally occupied by the Solution Explorer), with the new macro in it (see Figure 1-40). You can name the macro anything you like. Note that the Macro Explorer ships with several sample macros that you can "explore."

Macro Explorer

- n x

.¿J Macros

* .jjll MyMaeros

Modulel

* ijj Recording Module

-¿3 Tern fjuraiyMdt.ru

J kjU Samples

Run

[• ¿1} Accessibility

Edit

V AddDirAsSlnrolder

Rename

DevStudiob bditor

V J) MakeAddiri

X Doloto Od

t" JJ Utilities

t> VSDebugger

V g]VSEditor

Was this article helpful?

0 0

Post a comment