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 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 are called when a project is added, removed, or renamed within a solution:

 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 signatures of events fired when a project item is added to, removed from, or renamed within a project:

 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) 

To connect to these events, you can use the Events2 object's ProjectItemsEvents and ProjectsEvents properties. These properties will return an instance of an EnvDTE. ProjectItemsEvents or EnvDTE.ProjectsEvents object, which you can then use to receive event notifications from all projects or project items open within a solution. As new projects are added to the solution, or as new items are added to a project, the event handlers for your implementation of these interfaces are called. To connect to one of these events from the Macros IDE, simply open the EnvironmentEvents module of a macro project, and in the left drop-down at the top of the EnvironmentEvents source code window, select either the ProjectsEvents or ProjectItemsEvents item. Then in the right drop-down, select the event that you want to connect to.

You can connect to these events for all projects open in a solution and you can connect to them on a project type–specific basis. This means that if you want to connect to events on just Visual Basic projects loaded into a solution, or just on Visual C# projects, you can. Rather than using the Events2.ProjectItemsEvents and Events2.ProjectEvents methods to connect to these language type–specific events, you can use the Events.GetObject method, passing in a special string indicating the project type you want to connect to. But first, you need to set up the event variables to handle the event. You declare the event variable within a macro project by adding the following code to the EnvironmentEvents module. (This example connects to the Visual 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 Visual C# projects because the event variable, csharpProjectItemsEvents, has yet to be set to an instance of a ProjectItemsEvents object. To set this variable to an instance of the correct event object, create a handler for DTEEvents.OnStartupComplete and place 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 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; this way you will not need to restart Visual Studio 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 item 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 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 Visual C# smart device application. You can use eVBProjectsEvents and eVBProjectItemsEvents to capture events thrown by a Visual Basic 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.




Working with Microsoft Visual Studio 2005
Working with Microsoft Visual Studio 2005
ISBN: 0735623155
EAN: 2147483647
Year: 2006
Pages: 100

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net