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 Automating Repetitive Actions by Using Macros.
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 Responding to Automation Events.
Note
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 Visual Studio Settings.
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 Responding to Automation Events. In the EnvironmentEvents module, the module-level WindowEvents variable is already declared as shown below.
Public WithEvents windowEvents As EnvDTE.WindowEvents
In the EnvironmentEvents module, use the Class Name and Method Name drop-down boxes in the Macros IDE editor to create a WindowClosing event-handling routine for the variable, or cut and paste the code below.
Public Sub windowEvents_WindowClosing( _ ByVal Window As EnvDTE.Window) Handles windowEvents.WindowClosing MsgBox("You are closing the window.") End Sub
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 the Events property. This example provides a project-specific application of binding to ReferencesEvents. You can also provide a global-level event handler by defining the handler in the EnvironmentEvents module. For more information, see How to: Create an Event Handler in a Macro for a Specific Type of Project.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 to VSLangProj by 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.
Dim WithEvents refEvents As VSLangProj.ReferencesEvents
Use the Class Name and Method Name drop-down boxes in the editor to create event-handling routines for the variable.
Public Sub refEvents_ReferenceAdded(ByVal pReference As _ VSLangProj.Reference) Handles refEvents.ReferenceAdded MsgBox(pReference.Name & " was added to the project.") End Sub
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.
Public Sub ConnectRefEvents() Dim proj As VSLangProj.VSProject proj = CType(DTE.Solution.Projects.Item(1).Object, _ VSLangProj.VSProject) refEvents = proj.Events.ReferencesEvents End Sub
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."