Translate this site using Windows Live Translator:
How to: Respond to Events in a Specific Project (Visual C#) - A CRM Riff - Site Home - MSDN Blogs
A CRM Riff
It takes people to make the music of life

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

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

  • Comments 0

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.

 

To handle reference related events by using Visual C#

1.      Create a Visual Studio Professional edition add-in project in Visual C#.

2.      Add using VSLangProj; to the top of the Connect.cs file.

3.      On the Project menu, click Add Reference, click the .NET tab, select the first VSLangProj and click OK.

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.

[C#]

EnvDTE80.Events2 events =

(EnvDTE80.Events2)applicationObject.Events;

6.      In the Extensibility.IDTExtensibility2.OnConnection(System.Object,Extensibility.ext_ConnectMode,System.Object,System.Array@) method, initialize an EnvDTE.OutputWindow variable.

[C#]

OutputWindow outputWindow = (OutputWindow)applicationObject.Windows.Item

(Constants.vsWindowKindOutput).Object;

outputWinPane = outputWindow.OutputWindowPanes.Add

("ReferencesEvents Event Information");

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.

 

[C#]

using System;

using Microsoft.VisualStudio.CommandBars;

using Extensibility;

using EnvDTE;

using EnvDTE80;

using VSLangProj;

 

public class Connect : Object, Extensibility.IDTExtensibility2

{

  public Connect()

  {

  }

  public void OnConnection(object application, Extensibility.ext_ConnectMode connectMode, object addInInst, ref System.Array custom)

  {

    applicationObject = (_DTE)application;

    addInInstance = (AddIn)addInInst;

 

    // Retrieve the event objects from the automation model.

    EnvDTE80.Events2 events =

(EnvDTE80.Events2)applicationObject.Events;

    // Send event messages to the Output window.

    OutputWindow outputWindow =

 (OutputWindow)applicationObject.Windows.Item

(Constants.vsWindowKindOutput).Object;

    outputWinPane =

\outputWindow.OutputWindowPanes.Add

("ReferencesEvents Event Information");

 

    // Retrieve the event objects from the automation model.

    refEvents = (VSLangProj.ReferencesEvents)

events.GetObject("CSharpReferencesEvents");

 

    // Connect to each delegate exposed from each object

    // retrieved above.

    refEvents.ReferenceAdded += new

 _dispReferencesEvents_ReferenceAddedEventHandler

(this.ReferenceAdded);

    refEvents.ReferenceChanged += new

 _dispReferencesEvents_ReferenceChangedEventHandler

(this.ReferenceChanged);

    refEvents.ReferenceRemoved += new

 _dispReferencesEvents_ReferenceRemovedEventHandler

(this.ReferenceRemoved);

  }

 

  public void OnDisconnection(Extensibility.ext_DisconnectMode

 disconnectMode, ref System.Array custom)

  {

    // If the delegate handlers have been connected, then

    // disconnect them here.

    // If you do not do this, the handlers may still

    // fire because they have not been garbage collected.

    if (refEvents != null)

    {

        refEvents.ReferenceAdded -= new

 _dispReferencesEvents_ReferenceAddedEventHandler

(this.ReferenceAdded);

        refEvents.ReferenceChanged -= new

 _dispReferencesEvents_ReferenceChangedEventHandler

(this.ReferenceChanged);

        refEvents.ReferenceRemoved -= new

 _dispReferencesEvents_ReferenceRemovedEventHandler

(this.ReferenceRemoved);

    }

  }

 

  // References related events.

  public void ReferenceRemoved( VSLangProj.Reference removedRef )

  {

    outputWinPane.OutputString( "ReferencesEvents.ReferenceRemoved"

 + "\n" );

    outputWinPane.OutputString( "The reference to " + removedRef.Name

 + " was removed." + "\n" );

  }

 

  public void ReferenceChanged( VSLangProj.Reference changedRef )

  {

    outputWinPane.OutputString( "ReferencesEvents.ReferenceChanged"

+ "\n" );

    outputWinPane.OutputString( "The reference to " + changedRef.Name

+ " was changed." + "\n" );

  }

       

  public void ReferenceAdded( VSLangProj.Reference addedRef )

  {

    outputWinPane.OutputString( "ReferencesEvents.ReferenceAdded" +

 "\n" );

    outputWinPane.OutputString( "The reference to " + addedRef.Name

+ " was added." + "\n" );

  }

  public void OnAddInsUpdate(ref System.Array custom)

  {

  }

  public void OnStartupComplete(ref System.Array custom)

  {

  }

  public void OnBeginShutdown(ref System.Array custom)

  {

  }

  private _DTE applicationObject;

  private AddIn addInInstance;

  private VSLangProj.ReferencesEvents refEvents;

  private OutputWindowPane outputWinPane;

  }

}

Below is the code in Visual Basic:

[Visual Basic]

Imports System

Imports Microsoft.VisualStudio.CommandBars

Imports Extensibility

Imports EnvDTE

Imports EnvDTE80

Imports VSLangProj

 

Public Class Connect

    Implements IDTExtensibility2

    Dim applicationObject As DTE2

    Dim addInInstance As AddIn

    ' Handle Reference events.

    Public WithEvents refEvents As VSLangProj.ReferencesEvents

    Private outputWinPane As OutputWindowPane

Public Sub OnBeginShutdown(ByRef custom As System.Array)_

 Implements Extensibility.IDTExtensibility2.OnBeginShutdown

End Sub

 

Public Sub OnAddInsUpdate(ByRef custom As System.Array)_

 Implements Extensibility.IDTExtensibility2.OnAddInsUpdate

End Sub

 

Public Sub OnStartupComplete(ByRef custom As System.Array)_

 Implements Extensibility.IDTExtensibility2.OnStartupComplete

End Sub

 

Public Sub OnDisconnection(ByVal RemoveMode As Extensibility.ext_DisconnectMode, ByRef custom As System.Array)_

 Implements Extensibility.IDTExtensibility2.OnDisconnection

    ' Turns off reference event handling when the add-in shuts down.

    refEvents = Nothing

End Sub

 

Public Sub OnConnection(ByVal application As Object, ByVal connectMode_

 As Extensibility.ext_ConnectMode, ByVal addInInst As Object, ByRef_

 custom As System.Array) Implements_

 Extensibility.IDTExtensibility2.OnConnection

 

    applicationObject = CType(application, EnvDTE.DTE)

    addInInstance = CType(addInInst, EnvDTE.AddIn)

 

    Dim events As EnvDTE80.Events2

    events = CType(applicationObject.Events, Events2)

    ' Send event messages to the Output window.

    Dim outputWindow As OutputWindow

    outputWindow = CType(applicationObject.Windows.Item _

    (Constants.vsWindowKindOutput).Object, EnvDTE.OutputWindow)

    outputWinPane = outputWindow.OutputWindowPanes.Add("Reference_

    Event Information ")

 

    ' Retrieve the event objects from the automation model.

    ' Visual Basic automatically connects the method handler since

    ' the object variable declaration uses the 'WithEvents' handler.

    refEvents = CType(events.GetObject("CSharpReferencesEvents"),_

    ReferencesEvents)

End Sub

    ' Handle all of the various reference-related events.

    Sub ReferenceRemoved(ByVal removedRef As VSLangProj.Reference)_

    Handles refEvents.ReferenceRemoved

    outputWinPane.OutputString("ReferencesEvents.ReferenceRemoved" _

    & ControlChars.Lf)

    outputWinPane.OutputString("The reference to " & removedRef.Name _

    & " was removed." & ControlChars.Lf)

End Sub

 

Sub ReferenceChanged(ByVal changedRef As VSLangProj.Reference)_

    Handles refEvents.ReferenceChanged

    outputWinPane.OutputString("ReferencesEvents.ReferenceChanged" _

    & ControlChars.Lf)

    outputWinPane.OutputString("The reference to " & changedRef.Name _

    & " was changed." & ControlChars.Lf)

End Sub

Sub ReferenceAdded(ByVal addedRef As VSLangProj.Reference)_

    Handles refEvents.ReferenceAdded

    outputWinPane.OutputString("ReferencesEvents.ReferenceAdded" _

    & ControlChars.Lf)

    outputWinPane.OutputString("The reference to " & addedRef.Name _

    & " was added." & ControlChars.Lf)

End Sub

 

End Class

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.

 

Event Object Name

Description

VBBuildManagerEvents

Returns the VSLangProj.BuildManagerEvents object for Visual Basic projects.

VBImportsEvents

Returns the VSLangProj.ImportsEvents object for Visual Basic projects.

VBProjectItemsEvents

Returns the EnvDTE.ProjectItemsEvents object for Visual Basic projects.

VBProjectsEvents

Returns the EnvDTE.ProjectsEvents object for Visual Basic projects.

VBReferencesEvents

Returns the VSLangProj.ReferencesEvents object for Visual Basic projects.

WebReferencesEvents

Returns the VSLangProj80.WebReferencesEvents_ for Visual Basic, Visual C# and Visual J# projects.

VJSharpBuildManagagerEvents

Returns the VSLangProj.BuildManagerEvents object for Visual J# projects.

VJSharpProjectItemsEvents

Returns the EnvDTE.ProjectItemsEvents object for Visual J# projects.

VJSharpProjectsEvents

Returns the EnvDTE.ProjectsEvents object for Visual J# projects.

VJSharpReferencesEvents

Returns the VSLangProj.ReferencesEvents object for Visual J# projects.

CSharpBuildManagerEvents

Returns the VSLangProj.BuildManagerEvents object for Visual C# projects.

CSharpProjectItemsEvents

Returns the EnvDTE.ProjectItemsEvents object for Visual C# projects.

CSharpProjectsEvents

Returns the EnvDTE.ProjectsEvents object for Visual C# projects.

CSharpReferencesEvents

Returns the VSLangProj.ReferencesEvents object for Visual C# projects.

 

These strings can be found in the registry under the key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\8.0\Packages\<package GUID>. The Package GUIDs are:

164B10B9-B200-11D0-8C61-00A0C91E29D5 for Visual Basic project system.

39c9c826-8ef8-4079-8c95-428f5b1c323f for Web Developer Project System.

E6FDF8B0-F3D1-11D4-8576-0002A516ECE8 for Visual J#  project system.

FAE04EC1-301F-11D3-BF4B-00C04F79EFBC for Visual C# project system.

Note   These GUIDs are valid only in Visual Studio 2005.

 

Enjoy, Sirkku Willie, Programmer Writer

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.

Leave a Comment
  • Please add 6 and 3 and type the answer here:
  • Post