Export (0) Print
Expand All

Responding to IDE Events in a Macro (Visual Basic and Visual C# Projects)

Visual Studio .NET 2003

There are two types of events in the integrated development environment (IDE) — events that are language-neutral, and events that are language-dependent. Examples of language-neutral events include adding a new file, selecting a menu option, and closing a window. Language-specific events include adding a code file and adding a 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 Add Macro Project Dialog Box.

The following procedure describes responding to window events.

To capture a language-neutral event

  1. Create a new macro module, named CaptureEvents.
  2. Add a module-level WindowEvents variable.
    Dim WithEvents windowEvents As EnvDTE.WindowEvents
    
  3. Use the Class Name and Method Name drop-down boxes in the editor to create event-handling routines for the variable.
    Public Sub windowEvents_WindowClosing( _
    ByVal Window As EnvDTE.Window) Handles windowEvents.WindowClosing
       MsgBox("You are closing the window.")
    End Sub
    
  4. Add a method to initialize the variable. This macro must be run for the windowEvents_WindowClosing method to execute.
    Public Sub ConnectEvents()
       windowEvents = CType(DTE.Events, EnvDTE.WindowEvents)
    End Sub
    
  5. Save and run the ConnectEvents macro.

    Return to the development environment and close the active window. The message box will appear.

The following procedure describes responding to adding a reference to a project. The language-specific events are contained in the VSProject.Events property.

To capture a Visual Basic or Visual C# event

  1. Create a new macro module, CaptureRefEvents.
  2. Add a module-level ReferencesEvents variable.
    Dim WithEvents refEvents As VSLangProj.ReferencesEvents
    
  3. 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
    
  4. Add a method to initialize the 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
    
  5. Save and run the ConnectRefEvents macro.

See Also

Events Property (DTE Object) | Events Property (VSProject Object)

Show:
© 2014 Microsoft