Dela via


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 How to: Handle Environment Events in Macros.

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 the OnMacrosRuntimeReset and OnStartupComplete handlers to automate the event interception in the Visual Studio IDE.

This example adds a ReferencesEvents event handler that fires when a reference is added to a Visual C# or Visual Basic project.

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 Working with Settings.

To add a VSLangProj event handler to your macro

  1. In the Macros IDE, on the Project menu, click Add Reference.

  2. In the Add Reference dialog box, select VSLangProj, click Add, and then click OK.

  3. On the Class View explorer pane of the Macros IDE, expand MyMacros and double-click EnvironmentEvents to display it on the macro editor pane.

  4. On the top of the EnvironmentEvents module, add:

    Imports VSLangProj
    
  5. 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.

  6. Use the Class Name drop-down box to select the DTEEvents, 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.

  7. 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 project, replace the string CSharpReferencesEvents with VBReferencesEvents.

    Public Sub DTEEvents_OnMacrosRuntimeReset() Handles_
     DTEEvents.OnMacrosRuntimeReset
            EnvironmentEvents.refEvents_
            =CType(DTE.Events.GetObject("CSharpReferencesEvents")_
            , ReferencesEvents)
    End Sub
    

    and

    Private Sub DTEEvents_OnStartupComplete() Handles_
     DTEEvents.OnStartupComplete
            EnvironmentEvents.refEvents_
            = CType(DTE.Events.GetObject("CSharpReferencesEvents")_
            , ReferencesEvents)
    End Sub
    
  8. 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.

    Public Sub refEvents_ReferenceAdded(ByVal pReference As VSLangProj.Reference) Handles refEvents.ReferenceAdded
            MsgBox(pReference.Name & " was added to the project.")
    End Sub
    
  9. 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.

Example

This code shows the complete example. You can paste the code below in the EnvironmentEvents module of your macros project.

Note

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

See Also

Tasks

How to: Handle Events in a Macro

How to: Handle Automation Events (Visual Basic)

How to: Handle Automation Events (Visual C#)

Other Resources

Responding to Events (Visual Basic and Visual C# Projects)

Responding to Automation Events