Udostępnij za pośrednictwem


How to: Respond to Events in a Specific Project (Visual Basic)

The automation model includes objects that can be used to respond to environment events in the Visual Studio integrated development environment (IDE). The environment events defined in VSLangProj and VSLangProj80 are specific to Visual C# and Visual Basic projects. For example, ImportsEvents is raised when an import is added to or removed from an Imports collection.

This example uses Visual Basic to add a ReferencesEvents event handler, which is specific to a type of project, to an add-in project. ReferencesEvents are raised when a reference is changed, added to, or removed from a Visual C# or Visual Basic project.

Note

Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Customizing Development Settings in Visual Studio.

  1. Create a Visual Studio add-in project in Visual Basic.

  2. Add Imports VSLangProj to the top of the Connect.vb file.

  3. On the Project menu, click Add Reference, click the .NET tab, select the first VSLangProj, and click OK.

  4. In the Connect class, initialize a variable to handle the ReferencesEvents object and another to handle an OutputWindowPane.

    Public Class Connect
        Implements IDTExtensibility2
        Dim _applicationObject As DTE2
        Dim _addInInstance As AddIn
        ' Handle Reference events.
        Public WithEvents refEvents As VSLangProj.ReferencesEvents
        Private outputWinPane As OutputWindowPane
    

    In this example, the variable is named refEvents.

    Other objects in the automation model relate to other types of events that are specific to the project. For example, ImportsEvents is raised when an import is added to or removed from an Imports collection. BuildManagerEvents applies to events related to temporary assemblies built from outputs of custom tools. For more information on BuildManager object, see Introduction to the BuildManager Object. For a complete list of the events that are specific to types of projects, see Event Objects (Specific to Types of Projects). For a list of general automation events, see Automation Event Objects.

  5. In the OnConnection method, initialize a variable to intercept events. In this example, the variable is named events.

        Dim events As EnvDTE80.Events2
        events = CType(_applicationObject.Events, Events2)
    
  6. In the OnConnection method, initialize an OutputWindow variable.

        Dim outputWindow As OutputWindow
        outputWindow = CType(_applicationObject.Windows.Item _
        (Constants.vsWindowKindOutput).Object, EnvDTE.OutputWindow)
        outputWinPane = outputWindow.OutputWindowPanes.Add_
        ("Reference Event Information ")
    
  7. Also in the OnConnection method, retrieve the event objects from the automation model.

    refEvents = CType(events.GetObject("CSharpReferencesEvents"),_
     ReferencesEvents)
    

    Visual Studio automatically connects the method handler because the object variable declaration uses the WithEvents (Visual Basic) handler.

    In this example the ReferencesEvents are specific to Visual C# projects. To respond to events that are specific to Visual Basic events, replace the string, CSharpReferencesEvents with VBReferencesEvents. For more information about how to determine the strings to use for events that are specific to different types of projects, see Event Objects (Specific to Types of Projects).

  8. Add procedures for each event related to the event object. For example, to handle the event that occurs when a reference is added, you would use:

    Sub ReferenceAdded(ByVal addedRef As VSLangProj.Reference)_
        Handles refEvents.ReferenceAdded
        outputWinPane.OutputString_
        ("ReferencesEvents.ReferenceAdded" & ControlChars.Lf)
        outputWinPane.OutputString("The reference to " _
        & addedRef.Name & " was added." & ControlChars.Lf)
    End Sub
    

    In the case of ReferencesEvents, you must have events defined for ReferenceAdded, ReferenceRemoved, ReferenceChanged, as shown in the complete listing of the example below.

  9. Finally, to prevent Visual Studio from slowing your system by continuing to monitor window-related events after you close the add-in, you should disable event handling. In Visual Basic, this is done by setting the event handler to Nothing.

    Public Sub OnDisconnection(ByVal RemoveMode As_
     Extensibility.ext_DisconnectMode, ByRef custom As System.Array)_
     Implements Extensibility.IDTExtensibility2.OnDisconnection
        refEvents = Nothing
    End Sub
    

    This turns off event handling whether the add-in or the IDE is shut down while the add-in is still running. When the IDE is shut down, all running add-ins are automatically shut down first.

Example

The following example is a basic Visual Studio add-in that demonstrates how to intercept and handle Visual C# reference events in Visual Studio. Whenever a reference event occurs, a notification message is sent to the Output window.

Imports System
Imports Microsoft.VisualStudio.CommandBars
Imports Extensibility
Imports EnvDTE
Imports EnvDTE80
Imports VSLangProj

Public Class Connect
    Implements IDTExtensibility2
    Dim _applicationObject As DTE2
    Dim _addInInstance As AddIn
    ' Handle Reference events.
    Public WithEvents refEvents As VSLangProj.ReferencesEvents
    Private outputWinPane As OutputWindowPane
Public Sub OnBeginShutdown(ByRef custom As System.Array) _
 Implements Extensibility.IDTExtensibility2.OnBeginShutdown
End Sub

Public Sub OnAddInsUpdate(ByRef custom As System.Array) _
 Implements Extensibility.IDTExtensibility2.OnAddInsUpdate
End Sub

Public Sub OnStartupComplete(ByRef custom As System.Array) _
 Implements Extensibility.IDTExtensibility2.OnStartupComplete
End Sub

Public Sub OnDisconnection(ByVal RemoveMode As Extensibility.ext_DisconnectMode, ByRef custom As System.Array) _
 Implements Extensibility.IDTExtensibility2.OnDisconnection
    ' Turns off reference event handling when the add-in shuts down.
    refEvents = Nothing
End Sub

Public Sub OnConnection(ByVal application As Object, _
 ByVal connectMode As Extensibility.ext_ConnectMode, ByVal addInInst As Object, ByRef _
 custom As System.Array) Implements _
 Extensibility.IDTExtensibility2.OnConnection

    _applicationObject = CType(application, EnvDTE80.DTE2)
    _addInInstance = CType(addInInst, EnvDTE.AddIn)

    Dim events As EnvDTE80.Events2
    events = CType(_applicationObject.Events, Events2)
    ' Send event messages to the Output window.
    Dim outputWindow As OutputWindow
    outputWindow = CType(_applicationObject.Windows.Item _
    (Constants.vsWindowKindOutput).Object, EnvDTE.OutputWindow)
    outputWinPane = outputWindow.OutputWindowPanes.Add _
    ("Reference Event Information ")

    ' Retrieve the event objects from the automation model.
    ' Visual Basic automatically connects the method handler since 
    ' the object variable declaration uses the 'WithEvents' handler.
    refEvents = CType(events.GetObject("CSharpReferencesEvents"), _
    ReferencesEvents)
End Sub
    ' Handle all of the various reference-related events.
    Sub ReferenceRemoved(ByVal removedRef As VSLangProj.Reference) _
    Handles refEvents.ReferenceRemoved
    outputWinPane.OutputString("ReferencesEvents.ReferenceRemoved" _
    & ControlChars.Lf)
    outputWinPane.OutputString("The reference to " & removedRef.Name _
    & " was removed." & ControlChars.Lf)
End Sub

Sub ReferenceChanged(ByVal changedRef As VSLangProj.Reference) _
    Handles refEvents.ReferenceChanged
    outputWinPane.OutputString("ReferencesEvents.ReferenceChanged" _
    & ControlChars.Lf)
    outputWinPane.OutputString("The reference to " & changedRef.Name _
    & " was changed." & ControlChars.Lf)
End Sub
Sub ReferenceAdded(ByVal addedRef As VSLangProj.Reference) _
    Handles refEvents.ReferenceAdded
    outputWinPane.OutputString("ReferencesEvents.ReferenceAdded" _
    & ControlChars.Lf)
    outputWinPane.OutputString("The reference to " & addedRef.Name _
    & " was added." & ControlChars.Lf)
End Sub

End Class

Compiling the Code

To compile this code, create a new Visual Studio add-in project in Visual Basic and replace the code of the Connect class with the code in the example. For information about how to run an add-in, see How to: Control Add-Ins By Using the Add-In Manager.

See Also

Tasks

How to: Respond to Events in a Specific Project (Visual C#)

Reference

Imports Statement (.NET Namespace and Type)

Other Resources

Responding to Automation Events

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