Project and Project Item Events
Just as a solution fires events to allow an add-in or macro to respond to the actions the user is performing, the various project types also fire events so that an add-in or a macro can be informed of what the user is doing. You connect to the events fired by the different project types in different ways, but each project type supports the same interfaces used to handle the event invocations. Each project type fires two classes of events: actions performed with the project and actions performed with the items within those projects. Here are the events and the signatures that the project will fire:
void ItemAdded(ByVal Project As EnvDTE.Project)
void ItemRemoved(ByVal Project As EnvDTE.Project)
void ItemRenamed(ByVal Project As EnvDTE.Project,
ByVal OldName As String)
These are the available signatures of the project item events:
void ItemAdded(ByVal ProjectItem As EnvDTE.ProjectItem)
void ItemRemoved(ByVal ProjectItem As EnvDTE.ProjectItem)
void ItemRenamed(ByVal ProjectItem As EnvDTE.ProjectItem, _
ByVal OldName As String)
Project events and project item events both fire three separate eventsItemAdded, ItemRemoved, ItemRenamed. These signify that a project or a project item was added, removed, or renamed, respectively.Connecting to project items and projects events within a macro project requires more than connecting to other types of events supported by Visual Studio .NET. For example, to connect to solution events within a macro project, you first open the EnvironmentEvents module. Then, within this module, you select the SolutionEvents event object from the Class Name drop-down list on the left side of the text window and select a solution events handler method from the Method Name drop-down list. Connecting to project and project item events in a macro isn't this easy, however; you have to write a little glue code to connect the events. First, you declare the event variable by adding the following code to the EnvironmentEvents module. (This example uses the C# project events.)
<System.ContextStaticAttribute()> _
Public WithEvents csharpProjectItemsEvents As EnvDTE.ProjectItemsEvents
When you enter this code, an entry appears in the left drop-down list at the top of the code for the EnvironmentEvents macro module. Select the entry to fill the right drop-down list with the events for this object, and select each event to create the code necessary for capturing that event. At this point, the event handler won't be invoked for C# projects because the event variable, csharpProjectItemsEvents, has yet to be set to an instance of a ProjectItemsEvents object. You can set this variable to an instance of the correct event object by creating a handler for DTEEvents.OnStartupComplete and placing within it the code to connect to the event, much as you would within an add-in:
Private Sub DTEEvents_OnStartupComplete() _
Handles DTEEvents.OnStartupComplete
csharpProjectItemsEvents = _
DTE.Events.GetObject("CSharpProjectItemsEvents")
End Sub
With this event handler in place, when Visual Studio .NET is closed and then restarted, the OnStartupComplete handler will be invoked, which will cause the event variable to be connected. Of course, you can insert this same code into a macro and run the macro; Visual Studio .NET doesn't need to be restarted for the event variable to be set. Here's an example of such a macro:
Sub ConnectCSharpProjectItemsEvents()
csharpProjectItemsEvents = _
DTE.Events.GetObject("CSharpProjectItemsEvents")
End Sub
You can connect to the project and project items events for project types other than the Miscellaneous File and Solution Items projects by changing the string passed to the Events.GetObject method. For example, to connect to Visual Basic .NET project and project item events, you can use the strings VBProjectsEvents and VBProjectItemsEvents. You can use the strings VJSharpProjectsEvents and VJSharpProjectItemsEvents to connect to events thrown by a Microsoft Visual J# project, and you can use eCSharpProjectsEvents and eCSharpProjectItemsEvents to capture events thrown by a C# smart device application. You can use eVBProjectsEvents and eVBProjectItemsEvents to capture events thrown by a Visual Basic .NET smart device application. The ProjectEvents sample demonstrates how to connect to all these project and project item events. It connects to the events provided by each project type, and as each event is fired, a message box is displayed containing information about that event.