How to: Respond to Events in a Specific Project (Visual C#)

I am the newest programmer writer for the Visual Studio Automation team. This is my first blog, one that I think you will find interesting. This is new material that you normally wouldn’t see until the next release of Visual Studio 2005. Let me know if you have any questions.

The automation model includes objects that can be used to respond to environment events in the Visual Studio integrated development environment (IDE). The environment events defined in VSLangProj and VSLangProj80 are specific to Visual C#, Visual Basic, and Visual J# projects. For example, VSLangProj.ImportsEvents is raised when an import is added to, or removed from a Visual Basic project.

This example uses csprcs to add a VSLangProj.ReferencesEvents event handler, which is specific to a type of project, to an add-in project. VSLangProj.ReferencesEvents are raised when a reference is changed, added to, or removed from a Visual C#, Visual Basic, and Visual J# project.

4.In the Connect class, initialize a variable to handle the VSLangProj.ReferencesEvents object and another to handle an EnvDTE.OutputWindowPane.

[C#]

private DTE2 applicationObject;

private AddIn addInInstance;

private VSLangProj.ReferencesEvents refEvents;

private OutputWindowPane outputWinPane;

In this example, the variable is named refEvents.

Other objects in the automation model relate to other types of events that are specific to the project. For example, VSLangProj.ImportsEvents applies to events related to the Imports command in Visual Basic. VSLangProj.BuildManagerEvents applies to events related to the build. For a complete list of the events that are specific to types of projects, see Event Objects (Specific to Types of Projects), and for a list of the general automation events, see Automation Event Objects.

5.In the Extensibility.IDTExtensibility2.OnConnection(System.Object,Extensibility.ext_ConnectMode,System.Object,System.Array@) method, initialize a variable to intercept events. In this example, the variable is called events.

7.Also in the Extensibility.IDTExtensibility2.OnConnection(System.Object,Extensibility.ext_ConnectMode,System.Object,System.Array@) method, retrieve the event objects from the automation model.

[C#]

refEvents =

(VSLangProj.ReferencesEvents)events.GetObject

(“CSharpReferencesEvents”);

In this example the VSLangProj.ReferencesEvents are specific to Visual C# projects. To respond to Visual Basic, or Visual J# specific events, replace the string, CSharpReferencesEvents, with VBReferencesEvents or VJSharpReferencesEvents, respectively. For more information about how to determine the strings to use for events that are specific to different types of projects, see Listing of Project Type Specific Event Names below in the topic.

8.Connect to each delegate exposed from the event objects retrieved in step 3 by using the += operator. For example, to connect the delegates exposed by the VSLangProj.ReferencesEventsClass.ReferenceAdded event, you would use:

[C#]

refEvents.ReferenceAdded += new

_dispReferencesEvents_ReferenceAddedEventHandler

(this.ReferenceAdded);

9.Add procedures for each event that is related to the event object. For example, to handle the event that occurs when a reference is added, you would use:

[C#]

public void ReferenceAdded( VSLangProj.Reference addedRef )

{

outputWinPane.OutputString( “ReferencesEvents.ReferenceAdded”

+ “\n” );

outputWinPane.OutputString( “The reference to ” + addedRef.Name

+ ” was added.” + “\n” );

}

In the case of VSLangProj.ReferencesEvents, you must have events defined for:

·VSLangProj.ReferencesEventsClass.ReferenceAdded

·VSLangProj.ReferencesEventsClass.ReferenceRemoved

and

·VSLangProj.ReferencesEventsClass.ReferenceChanged.

The complete listing of the example below includes these events.

10.Finally, to prevent vsprvs from slowing your system by continuing to monitor window-related events after you close the add-in, you should disable event handling. In Visual C# this is done by using the -= operator. For example, to disable event handling for the VSLangProj.ReferencesEventsClass.ReferenceAdded you would use:

[C#]

refEvents.ReferenceAdded -= new

_dispReferencesEvents_ReferenceAddedEventHandler

(this.ReferenceAdded);

This turns off event handling whether the add-in or the IDE is shut down while the add-in is still running. When the IDE is shut down, all running add-ins are automatically shut down first.

The following example is a basic Visual Studio Professional edition add-in that demonstrates how to intercept and handle Visual C# reference events in Visual Studio Professional edition. Whenever a reference event occurs, a notification message is sent to the Output window.

To compile this code, create a new Visual Studio Professional edition add-in project and replace the code of the Connect class with the code in the example. For information about how to run an add-in, see How to: Control Add-ins with the Add-in Manager.

Important note: This material is provided in an ‘as is’ condition so that you might evaluate it for your own use at your own risk. You agree that by providing comments, suggestions, or other feedback related to our products, technology or services to the individuals listed above, you allow Microsoft, at its option, to use your feedback in our products, technology, and services without any obligation to you. Due to the volume of e-mails we receive, Microsoft, including the individual listed above, may not be able respond to your e-mail.