Re: Need help with intercepting the "Add reference" command

From: Carlos J. Quintero (carlosq_at_NOSPAMsogecable.com)
Date: 02/20/04


Date: Fri, 20 Feb 2004 12:10:46 +0100

It is difficult to say if you donīt post the minimal code to reproduce the
case, but I would say that:

> 1)
> I am starting the IDE, open project and click on Add reference the
> BeforeCommandExecute is NOT invoked.

This is normal since you have not clicked the menu item to capture the
events of the command

> 2)
> I am starting the IDE, clicked the menu item for registering, open project
> and click on Add reference the BeforeCommandExecute is NOT invoked.

This is not normal. Below you have the code to register command events
properly.

> 3)
> I am starting the IDE, open project, clicked the menu item for registering
> and click on Add reference the BeforeCommandExecute is invoked.

This is normal since you have clicked the menu item to capture the events of
the command

> When I am closing and reoping the project the BeforeCommandExecute
> is invoked every time.

Do you mean that the event is fired just opening and closing projects,
without adding references? This means that you are registering events for
more that one project command event.

The following code (VB.NET) shows how to trap events of a single command:

   Private m_objDTE As EnvDTE.DTE
   Private WithEvents m_objAddReferenceCommandEvents As CommandEvents

   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

      Dim objCommand As Command

      m_objDTE = CType(application, EnvDTE.DTE)

      objCommand = m_objDTE.Commands.Item("Project.AddReference")

      m_objAddReferenceCommandEvents =
m_objDTE.Events.CommandEvents(objCommand.Guid, objCommand.ID)

   End Sub

   Private Sub m_objAddReferenceCommandEvents_BeforeExecute(ByVal Guid As
String, ByVal ID As Integer, ByVal CustomIn As Object, ByVal CustomOut As
Object, ByRef CancelDefault As Boolean) Handles
m_objAddReferenceCommandEvents.BeforeExecute
      MessageBox.Show("Added reference")
   End Sub

Also, the technique that you are using (or the previous) captures
invocations to the Add Reference dialog, not actual references added. If you
want this last one, you can add a reference to the VSLangProj assembly, and
for each project of the loaded solution cast DTE.Project.Object to
VSLangProj.VSProject, get the Events.ReferencesEvents object and trap the
ReferenceAdded event.

-- 
Carlos J. Quintero (Visual Developer - .NET MVP)


Relevant Pages

  • Re: Access small text file in the IDE
    ... I'd like to access a small text file as a reference while in the IDE ... Is there a way to add a command to the toolbar to pop up the text file? ...
    (microsoft.public.vb.general.discussion)
  • Access small text file in the IDE
    ... I'd like to access a small text file as a reference while in the IDE ... Is there a way to add a command to the toolbar to pop up the text file? ...
    (microsoft.public.vb.general.discussion)
  • Re: Solution Build question / issue....
    ... compile in the IDE it spits out the LDAP key. ... compiled through the command line I get an Object Reference Error.... ... Any reason why the web project would not compile correctly? ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: database project
    ... There are two different DB reference in VS 2005 IDE. ... each connection is encrypted and stored on local for each ... The .dbp file is the Database Project file. ...
    (microsoft.public.vsnet.ide)
  • Re: Problem with referenced projects
    ... The "Could Not Copy Temporary Files to the Output Directory" Error Is ... And ervery reference has the property "Locale ... > same executing directory. ... > To temporally resolve the problem, i have to shutdown the IDE delete all ...
    (microsoft.public.vsnet.ide)