|Important||This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here.|
How to: Handle Events in a Macro
There are two types of events in the integrated development environment (IDE) — events that apply to all projects, and events that apply only to specific project types. Examples of events that apply to all project types include adding a new file, selecting a menu option, and closing a window. Examples of events that apply only to specific project types include adding a reference or a Web reference to a project.
This task assumes that you know how to access the Macros development environment and create a macro project. For more information, see.
This example demonstrates how to respond to window events, which apply to all Visual Studio projects. For more information about events that apply to all languages, see.
The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. These procedures were developed with the General Development Settings active. To change your settings, choose Import and Export Settings on the Tools menu. For more information, see.
To capture a language-neutral event
In MyMacros, create a new macro module and name it CaptureEvents.
When you create a new macro, a module named EnvironmentEvents is added by default. This module defines many of the event objects described in the table listed in. In the EnvironmentEvents module, the module-level WindowEvents variable is already declared as shown below.
In the EnvironmentEvents module, use the Class Name and Method Name drop-down boxes in the Macros IDE editor to create aevent-handling routine for the variable, or cut and paste the code below.
Return to the development environment and close the active window.
The message box appears. This message is displayed every time you close the active window. Delete the event handler if you do not want to be informed every time the active window is closed.
Adding a Reference to a Project
This example demonstrates how to respond to adding a reference to a project. The language-specific events are contained in theproperty. This example provides a project-specific application of binding to . You can also provide a global-level event handler by defining the handler in the EnvironmentEvents module. For more information, see .To run this example you must have a Visual Basic, Visual J#, or Visual C# project open in the Visual Studio IDE.
To capture a Visual Basic, Visual J#, or Visual C# event
Create a new macro module, CaptureRefEvents.
Add a reference toby selecting Add Reference… from the Project menu in the Macros Integrated Development Environment (IDE). On the Add Reference dialog box, select VSLangProj, click OK, and then Add.
In the EnvironmentEvents module, expand the Automatically generated code, do not modify collapsed section, and add a module-level ReferencesEvents variable.
Use the Class Name and Method Name drop-down boxes in the editor to create event-handling routines for the variable.
Right-click on CaptureRefEvents module in the Macro Explorer. Select New Macro from the drop-down menu. Cut and paste the code below to initialize the refEvents variable.
Save the ConnectRefEvents macro.
In the Visual Studio IDE, open a Visual Basic, Visual C# or a Visual J# project.
Run the ConnectRefEvents macro.
Add a reference to the project in the Visual Studio IDE.
A message box appears with the text: "<Reference name> was added to the project."