|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: Create an Event Handler in a Macro for a Specific Type of Project
The EnvironmentEvents template, which is included in every macro project, contains a number of pre-declared event handlers. These event handlers can be implemented to respond to a number of environment events, such as a window closing, and so on. Once the implementation is provided, these general extensibility event handlers automatically intercept a specified event every time it takes place in the Visual Studio integrated development environment (IDE). For more information, see.
You can also add your own IDE event handlers to the EnvironmentEvents template. You declare an event variable, provide the event handler code for it, and add theand handlers to automate the event interception in the Visual Studio IDE.
This example adds aevent handler that fires when a reference is added to a Visual C#, Visual Basic, or Visual J# project.
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 add a VSLangProj event handler to your macro
In the Macros IDE, on the Project menu, click Add Reference.
In the Add Reference dialog box, select VSLangProj, click Add, and then click OK.
On the Class View explorer pane of the Macros IDE, expand MyMacros and double-click EnvironmentEvents to display it on the macro editor pane.
On the top of the EnvironmentEvents module, add:
Enter a declaration for the event after the hidden region of the EnvironmentEvents module labeled "Automatically generated code, do not modify":
<System.ContextStaticAttribute()> Public WithEvents refEvents As VSLangProj.ReferencesEvents
This declaration adds ReferencesEvents and its methods to the Class Name and Method Name drop-down boxes in the Macros IDE code editor.
Use the Class Name drop-down box to select the, and Method Name drop-down box to select OnMacrosRuntimeReset and OnStartupComplete.
This creates event handlers for these two events. These event handlers are necessary for your ReferencesEvents handler to automatically intercept events every time they take place in the Visual Studio IDE.
Modify the event handlers by adding the EnvironmentEvents.refEvents cast, as shown below. To intercept an event when a reference is added to a Visual Basic or Visual J# project, replace the string CSharpReferencesEvents with VBReferencesEvents or VJSharpReferencesEvents ,respectively.
Public Sub DTEEvents_OnMacrosRuntimeReset() Handles_ DTEEvents.OnMacrosRuntimeReset EnvironmentEvents.refEvents_ =CType(DTE.Events.GetObject("CSharpReferencesEvents")_ , ReferencesEvents) End Sub
Finally, paste the following code in the EnvironmentEvents module, or select refEvents from the Class Name drop-down box, and ReferenceAdded from the Method Name drop-down box to create an event handler. Add the line, MsgBox(pReference.Name & " was added to the project."), to the event handler as shown below.
To test your event handler, create a new Visual C# project of any kind.
A message box appears for each reference that is automatically added to the project template.
This code shows the complete example. You can paste the code below in the EnvironmentEvents module of your macros project.
After this event handler is added, it fires every time a reference is added to the specified project type. If you do not want to be notified every time a reference is added, comment out this code.
<System.ContextStaticAttribute()> Public WithEvents refEvents As VSLangProj.ReferencesEvents Public Sub DTEEvents_OnMacrosRuntimeReset() Handles_ DTEEvents.OnMacrosRuntimeReset EnvironmentEvents.refEvents= _ CType(DTE.Events.GetObject("CSharpReferencesEvents")_ , VSLangProj.ReferencesEvents) End Sub Private Sub DTEEvents_OnStartupComplete() Handles_ DTEEvents.OnStartupComplete EnvironmentEvents.refEvents = _ CType(DTE.Events.GetObject("CSharpReferencesEvents")_ , VSLangProj.ReferencesEvents) End Sub Public Sub refEvents_ReferenceAdded(ByVal pReference As VSLangProj.Reference) Handles refEvents.ReferenceAdded MsgBox(pReference.Name & " was added to the project.") End Sub