方法 : 特定のプロジェクトのイベントに応答する (Visual Basic)

更新 : 2007 年 11 月

オートメーション モデルには、Visual Studio 統合開発環境 (IDE: Integrated Development Environment) で環境イベントへの応答に使用できるオブジェクトが含まれています。VSLangProj および VSLangProj80 で定義された環境イベントは、Visual C#、Visual Basic、および Visual J# の各プロジェクトに固有です。たとえば、ImportsEvents は、Imports コレクションに対しインポートの追加または削除を行ったときに発生します。

この例では、Visual Basic を使用し、プロジェクトの種類に固有の ReferencesEvents イベント ハンドラをアドイン プロジェクトに追加します。ReferencesEvents は、Visual C#、Visual Basic、または Visual J# のプロジェクトで参照を変更、追加、または削除したときに発生します。

ms228947.alert_note(ja-jp,VS.90).gifメモ :

使用している設定またはエディションによっては、表示されるダイアログ ボックスやメニュー コマンドがヘルプに記載されている内容と異なる場合があります。ここに記載されている手順は、全般的な開発設定が適用されているものとして記述されています。設定を変更するには、[ツール] メニューの [設定のインポートとエクスポート] をクリックします。詳細については、「Visual Studio の設定」を参照してください。

Visual Basic を使用して参照に関連するイベントを処理するには

  1. Visual Basic で Visual Studio のアドイン プロジェクトを作成します。

  2. Connect.vb ファイルの先頭に Imports VSLangProj を追加します。

  3. [プロジェクト] メニューの [参照の追加] をクリックし、[.NET] タブをクリックします。最初の [VSLangProj] を選択し、[OK] をクリックします。

  4. Connect クラスで、ReferencesEvents オブジェクトを処理する変数および 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
    

    この例では、変数の名前は refEvents です。

    オートメーション モデル内の他のオブジェクトは、プロジェクト固有の他の種類のイベントに関連しています。たとえば、ImportsEvents は、Imports コレクションに対してインポートの追加または削除を行ったときに発生します。BuildManagerEvents は、カスタム ツールの出力で作成された一時的なアセンブリに関連するイベントに適用されます。BuildManager オブジェクトの詳細については、「BuildManager オブジェクトの概要」を参照してください。プロジェクトの種類に固有のイベントの一覧については、「イベント オブジェクト (プロジェクトの種類に固有)」を参照してください。一般的なオートメーション イベントの一覧については、「オートメーション イベント オブジェクト」を参照してください。

  5. OnConnection メソッドでは、変数を初期化してイベントを受け取ります。次の例では、変数がイベントの名前になります。

        Dim events As EnvDTE80.Events2
        events = CType(_applicationObject.Events, Events2)
    
  6. OnConnection メソッドで、OutputWindow 変数を初期化します。

        Dim outputWindow As OutputWindow
        outputWindow = CType(_applicationObject.Windows.Item _
        (Constants.vsWindowKindOutput).Object, EnvDTE.OutputWindow)
        outputWinPane = outputWindow.OutputWindowPanes.Add_
        ("Reference Event Information ")
    
  7. また、OnConnection メソッドでは、オートメーション モデルからイベント オブジェクトを取得します。

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

    オブジェクト変数の宣言で WithEvents ハンドラが使用されているため、Visual Studio は自動的にメソッド ハンドラに接続されます。

    この例では、ReferencesEvents は Visual C# プロジェクトに固有のオブジェクトです。Visual Basic または Visual J# のプロジェクトに固有のイベントに応答するには、CSharpReferencesEvents という文字列をそれぞれ、VBReferencesEvents または VJSharpReferencesEvents に置き換えます。さまざまな種類のプロジェクトに固有のイベントに使用する文字列を判断する方法の詳細については、「イベント オブジェクト (プロジェクトの種類に固有)」を参照してください。

  8. イベント オブジェクトに関連する各イベントにプロシージャを追加します。たとえば、参照が追加されたときに発生するイベントを処理するには、次のプロシージャを追加します。

    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
    

    ReferencesEvents の場合は、後述の完全な使用例に示すように、ReferenceAddedReferenceRemoved、および ReferenceChanged に対してイベントを定義しておく必要があります。

  9. 最終的に、アドインの終了後もウィンドウに関連するイベントを引き続き監視して、Visual Studio によってシステムの処理速度が低下しないようにするには、イベント処理を無効にする必要があります。Visual Basic でイベント処理を無効にするには、イベント ハンドラを Nothing に設定します。

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

    これにより、アドインを実行したままアドインまたは IDE がシャットダウンされるかどうかに関係なく、イベント処理が無効になります。IDE がシャットダウンされると、最初に、実行中のすべてのアドインが自動的にシャットダウンされます。

使用例

基本的な Visual Studio アドインの例を次に示します。この例では、Visual Studio で Visual C# 参照イベントを受け取って処理する方法について説明します。参照イベントが発生した場合は、必ず通知メッセージが [出力] ウィンドウに送信されます。

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

コードのコンパイル方法

このコードをコンパイルするには、Visual Basic で新しい Visual Studio アドイン プロジェクトを作成し、Connect クラスのコードを例のコードで置き換えます。アドインの実行方法については、「方法 : アドイン マネージャを使用してアドインを制御する」を参照してください。

参照

処理手順

方法 : 特定のプロジェクトのイベントに応答する (Visual C#)

参照

Imports ステートメント (.NET 名前空間および型)

その他の技術情報

オートメーション イベントへの応答

イベントへの応答 (Visual Basic および Visual C# プロジェクト)