共用方式為


HOW TO:使用巨集處理事件

更新:2007 年 11 月

當您建立新的巨集時,會預設加入一個名為 EnvironmentEvents 的模組。這個模組會預先定義許多在回應 Automation 事件所列的表格中描述的事件物件 (Event Object),所以您不必自己定義。如果您想要處理的事件不在 EnvironmentEvents 模組中,可以自行加入。如需如何執行此工作的詳細資訊,請參閱 HOW TO:處理巨集中的環境事件

在下列程序中,會示範如何使用巨集處理與工具視窗 (在此範例中為 [工作清單]) 相關的事件。

注意事項:

根據目前使用的設定與版本,您所看到的對話方塊與功能表命令可能會與 [說明] 中所描述的不同。使用 [一般開發設定] 開發了這些程序。若要變更設定,請從 [工具] 功能表中選擇 [匯入和匯出設定]。如需詳細資訊,請參閱 Visual Studio 設定

若要處理與工具視窗相關的事件

  1. 將下列程式碼加入至 [巨集 IDE] 中的 EnvironmentEvents 模組。

  2. 執行巨集。

    當巨集在 [工作清單] 中加入項目及移除項目時,就會處理 [工作清單] 事件。

範例

在下列巨集範例中,會示範如何使用 Visual Studio Automation 模型事件物件回應事件。這個範例會在 [工作清單] 中加入工作及移除工作,以回應事件處理常式中的事件。

' Macro code.
Public Module EnvironmentEvents

   Public Sub TaskListEvents_TaskAdded(ByVal TaskItem As _
   EnvDTE.TaskItem) Handles TaskListEvents.TaskAdded
      MsgBox("A task named '" & TaskItem.Description & "' was added _
      to the Task List.")
      ' Put other code here that you want to execute when this 
      ' event occurs.
   End Sub

   Public Sub TaskListEvents_TaskRemoved(ByVal TaskItem As _
   EnvDTE.TaskItem) Handles TaskListEvents.TaskRemoved
      MsgBox("A task named '" & TaskItem.Description & "' was _
      removed from the Task List.")
      ' Put other code here that you want to execute when this 
      ' event occurs.
   End Sub
End Module

Sub EventsExample()
    ' Add items to and remove items from the Task List.
    Dim TL As TaskList = dte.ToolWindows.TaskList
    Dim TLItem As taskitem

    ' Add a couple of tasks to the Task List.
    TLItem = TL.TaskItems.Add(" ", " ", "Test task 1.", _
    vsTaskPriority.vsTaskPriorityHigh, vsTaskIcon.vsTaskIconUser, _
    True, , 10, , )
    TLItem = TL.TaskItems.Add(" ", " ", "Test task 2.", _
    vsTaskPriority.vsTaskPriorityLow, vsTaskIcon.vsTaskIconComment, _
    , , 20, , )

    ' List the total number of task list items after adding the new 
    ' task items.
    MsgBox("Task Item 1 description: " & _
    TL.TaskItems.Item(2).Description)
    MsgBox("Total number of task items: " & TL.TaskItems.Count)

    ' Remove the second task item. The items list in reverse numeric 
    ' order.
    MsgBox("Deleting the second task item")
    TL.TaskItems.Item(1).Delete()
    MsgBox("Total number of task items: " & TL.TaskItems.Count)
End Sub

若要回應增益集 (Add-In) 中的事件,請在 OnConnectionMethod 事件中初始化事件處理常式。例如:

Public Class Class1
   Implements IDTExtensibility2
   Dim objEventsClass As EventsClass

   Public Sub OnConnection(ByVal Application As Object, ByVal _
   ConnectMode As EnvDTE.ext_ConnectMode, ByVal AddInInst As _
   Object, ByRef custom() As Object) Implements _
   EnvDTE.IDTExtensibility2.OnConnection
      objEventsClass = New EventsClass()
      objEventsClass.TaskListEvents = _
      _applicationObj.Events.TaskListEvents
   End Sub
End Class

Public Class EventsClass
   Public WithEvents TaskListEvents As EnvDTE.TaskListEvents

   Private Sub TaskListEvents_TaskAdded(ByVal TaskItem As _
   EnvDTE.TaskItem) Handles TaskListEvents.TaskAdded
      MsgBox("A task named '" & TaskItem.Description & "' was added _
      to the Task List.")
      ' Put any other code here that you want to execute when this 
      ' event occurs.
   End Sub
End Class

請參閱

其他資源

回應事件 (Visual Basic 和 Visual C# 專案)