Logo
HOWTO: Initializing new events in Visual Studio macros

Author: Carlos J. Quintero (Microsoft MVP) Applies to: Microsoft Visual Studio .NET 2002
Date: March 2007   Microsoft Visual Studio .NET 2003
Updated: March 2013   Microsoft Visual Studio 2005
      Microsoft Visual Studio 2008
      Microsoft Visual Studio 2010

Introduction

This article explains how to initialize new event handlers not provided in the EnvironmentEvents file of a macros project. 

More Information

Each macros project has an EnvironmentEvents file that provides declaration of common events such as:

Public Module EnvironmentEvents
#Region "Automatically generated code, do not modify"
'Automatically generated code, do not modify
'Event Sources Begin
   <System.ContextStaticAttribute()> Public WithEvents DTEEvents As EnvDTE.DTEEvents
   <System.ContextStaticAttribute()> Public WithEvents DocumentEvents As EnvDTE.DocumentEvents
   <System.ContextStaticAttribute()> Public WithEvents WindowEvents As EnvDTE.WindowEvents
   <System.ContextStaticAttribute()> Public WithEvents TaskListEvents As EnvDTE.TaskListEvents
   <System.ContextStaticAttribute()> Public WithEvents FindEvents As EnvDTE.FindEvents
   <System.ContextStaticAttribute()> Public WithEvents OutputWindowEvents As EnvDTE.OutputWindowEvents
   <System.ContextStaticAttribute()> Public WithEvents SelectionEvents As EnvDTE.SelectionEvents
   <System.ContextStaticAttribute()> Public WithEvents BuildEvents As EnvDTE.BuildEvents
   <System.ContextStaticAttribute()> Public WithEvents SolutionEvents As EnvDTE.SolutionEvents
   <System.ContextStaticAttribute()> Public WithEvents SolutionItemsEvents As EnvDTE.ProjectItemsEvents
   <System.ContextStaticAttribute()> Public WithEvents MiscFilesEvents As EnvDTE.ProjectItemsEvents
   <System.ContextStaticAttribute()> Public WithEvents DebuggerEvents As EnvDTE.DebuggerEvents
   <System.ContextStaticAttribute()> Public WithEvents ProjectsEvents As EnvDTE.ProjectsEvents
   <System.ContextStaticAttribute()> Public WithEvents TextDocumentKeyPressEvents As EnvDTE80.TextDocumentKeyPressEvents
   <System.ContextStaticAttribute()> Public WithEvents CodeModelEvents As EnvDTE80.CodeModelEvents
   <System.ContextStaticAttribute()> Public WithEvents DebuggerProcessEvents As EnvDTE80.DebuggerProcessEvents
   <System.ContextStaticAttribute()> Public WithEvents DebuggerExpressionEvaluationEvents As EnvDTE80.DebuggerExpressionEvaluationEvents
'Event Sources End
'End of automatically generated code
#End Region

End Module

These events are declared in a code region generated automatically that you should not modify, and they are somehow initialized automatically by the Visual Studio environment (otherwise they would be Nothing). However, sometimes you need macros for event handlers not declared there, for example for project items events. To declare and initialize them you have to use this code outside that region:

<System.ContextStaticAttribute()> Public WithEvents VBProjectItemsEvents As EnvDTE.ProjectItemsEvents
<System.ContextStaticAttribute()> Public WithEvents CSharpProjectItemsEvents As EnvDTE.ProjectItemsEvents
Private Sub DTEEvents_OnMacrosRuntimeReset() Handles DTEEvents.OnMacrosRuntimeReset
   MsgBox("DTEEvents_OnMacrosRuntimeReset")
   InitializeEventHandlers()
End Sub
Private Sub DTEEvents_OnStartupComplete() Handles DTEEvents.OnStartupComplete
   MsgBox("DTEEvents_OnStartupComplete")
   InitializeEventHandlers()
End Sub
Private Sub InitializeEventHandlers()
   VBProjectItemsEvents = CType(DTE.Events.GetObject("VBProjectItemsEvents"), ProjectItemsEvents)
   CSharpProjectItemsEvents = CType(DTE.Events.GetObject("CSharpProjectItemsEvents"), ProjectItemsEvents)
End Sub
Private Sub ProjectItemsEvents_ItemAdded(ByVal ProjectItem As EnvDTE.ProjectItem) _
   Handles VBProjectItemsEvents.ItemAdded, CSharpProjectItemsEvents.ItemAdded
   MsgBox(ProjectItem.Name & " added")
End Sub

Notice that the events are initialized when the IDE is loaded (DTEEvents_OnStartupComplete) or when the macros runtime environment is reset (DTEEvents_OnMacrosRuntimeReset).


Go back to the 'Resources for Visual Studio .NET extensibility' section for more articles like this


Top