How can I use the ProjectItemsEvents to get notification when an item is added to a VB or C# project, in an add-in? This is a problem that I struggled with in my early add-in days, and because I could not figure how to get the ProjectItemsEvents to work properly, I went around the world to figure out how to know when a Form or Class was added to the project.
Like many areas of extensibility in Visual Studio .NET, MSDN documentation is not very helpful on this particular topic. In this article, I will show you that it is very simple to do, once you see how it is done. I will illustrate through the description of some code taken directly from an add-in.
First, I will declare two events; one for trapping the addition of a VB Project Item and the other for a C# project.
| Public Shared oVS As EnvDTE.DTE Public WithEvents eventsPIVB As EnvDTE.ProjectItemsEvents Public WithEvents eventsPICSharp As EnvDTE.ProjectItemsEvents Dim events As EnvDTE.Events |
| ' sink the event handlers for events events = oVS.Events eventsPIVB = oVS.Events.GetObject("VBProjectItemsEvents") eventsPICSharp = oVS.Events.GetObject("CSharpProjectItemsEvents") |
| Private Sub eventsPIVB_ItemAdded(ByVal ProjectItem As EnvDTE.ProjectItem) _ Handles eventsPIVB.ItemAdded Debug.Write("ProjectItemAdded: " & ProjectItem.Name) End Sub Private Sub eventsPICSharp_ItemAdded(ByVal ProjectItem As EnvDTE.ProjectItem) _ Handles eventsPICSharp.ItemAdded Debug.Write("ProjectItemAdded: " & ProjectItem.Name) End Sub |