Export (0) Print
Expand All

Responding to Events

When events occur in elements of the Visual Studio .NET integrated development environment (IDE), you can use objects from the automation model to respond to them. The following is a list of the event objects in the Visual Studio .NET automation model.

Object Name Description
BuildEvents Object Provides events for solution builds.
CommandBarEvents Object Provides events for controls on the Command Bar.
CommandEvents Object Provides command events for Add-ins.
DebuggerEvents Object Provides events for the debugger.
DocumentEvents Object Provides events for documents in the IDE.
DTEEvents Object Provides events for the state of the IDE.
Events Object Represents all events in the automation model.
FindEvents Object Provides events for text and file search operations.
OutputWindowEvents Object Provides events for the Output window.
ProjectItemsEvents Object Provides events for project items.
ProjectsEvents Object Provides events for projects.
SelectionEvents Object Provides events for changes made to a selection.
SolutionEvents Object Provides events for solutions.
TaskListEvents Object Provides events for the Task List.
TextEditorEvents Object Provides events for when document text is changed.
WindowEvents Object Provides events for windows.

You can use them, for example, to respond when:

  • A build is initiated or completed.
  • Someone clicks a command bar button.
  • A document is opened, closed, or saved.
  • Items are added, renamed, or removed from a solution or project.
  • A line is changed in the Code Editor.
  • A window is created, selected, moved, or closed.
Note   When you create a new macro, a module named "EnvironmentEvents" is added by default; this module defines many of the event objects described in the preceding table. When responding to events in VSA code, you can use the EnvironmentEvents module to create the event handlers for you. For detail about how to use the EnvironmentEvents module in the Macros IDE, see Macros and the EnvironmentEvents Project Item.

Events Examples

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

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 any 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 any other code here that you want to execute when this event 
      ' occurs.
   End Sub
End Module

Sub EventsExample()
   ' Add and remove items to/from the Task List.
   Dim win As Window = DTE.Windows.Item(Constants.vsWindowKindTaskList)
   Dim TL As TaskList = win.Object
   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

Controlling Projects and Solutions | Adding and Handling Commands | Creating and Controlling Environment Windows | Creating Add-Ins and Wizards | Creating an Add-In | Creating a Wizard | Automation and Extensibility Reference | Automation Object Model Chart

Show:
© 2014 Microsoft