Responding to IDE Events


Visual Basic .NET Unleashed
By Paul Kimmel
Table of Contents
Chapter 4.  Macros and Visual Studio Extensibility

Responding to IDE Events

The DTE has exposed several categories of events for which you can write event handlers, enabling you to write code that fires when something in the VS .NET IDE occurs rather than requiring the user to explicitly start a macro.

Categories of events include DTEEvents, DocumentEvents, WindowEvents, TaskListEvents, FindEvents, OutputWindowEvents, SelectionEvents, BuildEvents, SolutionEvents, SolutionItemEvents, MiscFilesEvents, and DebuggerEvents. For example, DTEEvents has an OnStartupComplete event that fires when a macro project is loaded. WindowEvents passes a reference to a closing EnvDTE.Window object when you close a window in the VS .NET IDE. BuildEvents will notify your macro code when a build has begun and finished.

The subject of macros is probably one that can benefit from a book of its own. The number of event objects and associated member events available to macro authors is huge. If you think you need to respond to a macro event, filter the help engine on Visual Studio Macros and begin your search based on the event objects listed in the second paragraph of this section.

The mechanism for implementing event handlers in the Macros IDE is that you select your macro project in the macro Project Explorer and find the EnvironmentEvents module; double-click to open it. In the code editor view for the EnvironmentEvents module, select a Class Name from the left combo box and the event you want to implement from the Method Name , or right combo box. Write the code interacting with parameters and available objects similar to what you would do if you were implementing a VB .NET application. The fragment that follows implements the DTEEvents OnStartupComplete event.

 Public Sub DTEEvents_OnStartupComplete() _   Handles DTEEvents.OnStartupComplete   MsgBox("Custom Event Handler: Macro Project Loaded") End Sub 

The OnStartupComplete event displays a message box indicating that the event project containing this EnvironmentEvents module has finished loading. The four procedures in Listing 4.6 demonstrate code that responds to an application build.

Listing 4.6 Code that responds when you build your application
  1:  Private Const Started As String = _  2:  "Build {0}  started at {1} "  3:  Private Const Ended As String = _  4:  "Build {0}  finished at {1} "  5:   6:  Private Function GetBuildStarted()  7:  Return String.Format(Started, _  8:  DTE.Solution.FullName, Now)  9:  End Function  10:   11:  Private Function GetBuildEnded()  12:  Return String.Format(Ended, _  13:  DTE.Solution.FullName, Now)  14:  End Function  15:   16:  Public Sub BuildEvents_OnBuildBegin(_  17:  ByVal Scope As EnvDTE.vsBuildScope, _  18:  ByVal Action As EnvDTE.vsBuildAction) _  19:  Handles BuildEvents.OnBuildBegin  20:   21:  EventLog.WriteEntry("application", GetBuildStarted())  22:   23:  End Sub  24:   25:  Public Sub BuildEvents_OnBuildDone(_  26:  ByVal Scope As EnvDTE.vsBuildScope, _  27:  ByVal Action As EnvDTE.vsBuildAction) _  28:  Handles BuildEvents.OnBuildDone  29:   30:  EventLog.WriteEntry("application", GetBuildEnded())  31:  End Sub 

Implement OnBuildBegin and OnBuildDone events for the BuildEvents object and you could add an event log entry as demonstrated on lines 21 and 30, logging the start and finish time of a build using the shared method EventLog.WriteEntry.

As mentioned, you can respond to a lot of events. You probably will not write a significant number of IDE events, but they may be especially helpful on occasion. For now let's proceed to the subject of extensibility.


Visual BasicR. NET Unleashed
Visual BasicR. NET Unleashed
Year: 2001
Pages: 222 © 2008-2017.
If you may any questions please contact us: