Поделиться через


How to: Handle Events by Using Macros

When you create a new macro, a module named EnvironmentEvents is added by default. This module predefines many of the event objects described in the table listed in Responding to Automation Events so you do not need to do it yourself. If you want to handle an event that is not in the EnvironmentEvents module, you can add it. For details about how to do this, see How to: Handle Environment Events in Macros.

The procedure below demonstrates how to use a macro to handle events that are related to a tool window (in this case, the Task List).

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.

  1. Add the following code to the EnvironmentEvents module in the Macros IDE.

  2. Run the macro.

    As the macro adds items to and removes items from the Task List, the Task List events are handled.

Example

The following macro example demonstrates how to respond to an event by using the Visual Studio automation model event objects. This example adds tasks to and removes tasks from the Task List, responding to the events in the event handlers.

' 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

To respond to events in add-ins, initialize the event handler in the OnConnectionMethod event. For example:

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

See Also

Other Resources

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