You need to create a receiver for these events.
Implementing the _dispVBProjectsEvents dispatch _dispVBProjectsEvents is an implementation that responds to these events by triggering regular .net events, effectively wrapping VBProjects events:
public class VBProjectsEventsSink : _dispVBProjectsEvents { public event EventHandler<DispatcherEventArgs<VBProject>> ProjectAdded; public void ItemAdded(VBProject VBProject) { if (VBProject.Protection == vbext_ProjectProtection.vbext_pp_none) { OnDispatch(ProjectAdded, VBProject); } } public event EventHandler<DispatcherEventArgs<VBProject>> ProjectRemoved; public void ItemRemoved(VBProject VBProject) { if (VBProject.Protection == vbext_ProjectProtection.vbext_pp_none) { OnDispatch(ProjectRemoved, VBProject); } } public event EventHandler<DispatcherRenamedEventArgs<VBProject>> ProjectRenamed; public void ItemRenamed(VBProject VBProject, string OldName) { var handler = ProjectRenamed; if (handler != null && VBProject.Protection == vbext_ProjectProtection.vbext_pp_none) { handler.Invoke(this, new DispatcherRenamedEventArgs<VBProject>(VBProject, OldName)); } } public event EventHandler<DispatcherEventArgs<VBProject>> ProjectActivated; public void ItemActivated(VBProject VBProject) { if (VBProject.Protection == vbext_ProjectProtection.vbext_pp_none) { OnDispatch(ProjectActivated, VBProject); } } private void OnDispatch(EventHandler<DispatcherEventArgs<VBProject>> dispatched, VBProject project) { var handler = dispatched; if (handler != null) { handler.Invoke(this, new DispatcherEventArgs<VBProject>(project)); } } }
The DispatcherEventArgs class is just a convenient way to show the VBProject element associated with the event and can be reused for other receivers:
public class DispatcherEventArgs<T> : EventArgs where T : class { private readonly T _item; public DispatcherEventArgs(T item) { _item = item; } public T Item { get { return _item; } } }
The client code must register the receiver - and for this you need to save the IConnectionPoint field and its int cookie:
private readonly IConnectionPoint _projectsEventsConnectionPoint; private readonly int _projectsEventsCookie;
The VBProjects collection implements the IConnectionPointContainer interface, which must be used to find the connection point:
var sink = new VBProjectsEventsSink(); var connectionPointContainer = (IConnectionPointContainer)_vbe.VBProjects; var interfaceId = typeof (_dispVBProjectsEvents).GUID; connectionPointContainer.FindConnectionPoint(ref interfaceId, out _projectsEventsConnectionPoint);
Once you have IConnectionPoint , use the Advise method to βconnectβ your receiver and receive a cookie:
_projectsEventsConnectionPoint.Advise(sink, out _projectsEventsCookie);
And then you can handle the events of the receiver, like any "regular" .net events:
sink.ProjectAdded += sink_ProjectAdded; sink.ProjectRemoved += sink_ProjectRemoved; sink.ProjectActivated += sink_ProjectActivated; sink.ProjectRenamed += sink_ProjectRenamed;
If you want to disconnect your receiver, you pass the cookie to the Unadvise method of the Unadvise instance:
_projectsEventsConnectionPoint.Unadvise(_projectsEventsCookie);
"Just!"