| Author: |
Carlos J. Quintero (Microsoft MVP) |
Applies to: |
Microsoft Visual Studio .NET 2002 |
| Date: |
April 2004 |
|
Microsoft Visual Studio .NET 2003 |
| Updated: |
February 2009 |
|
|
| |
|
|
|
Introduction
When two add-ins set event handlers to the same command of the Visual Studio
.NET IDE, only the last one loaded receives events from that command.
Cause
This is a bug of Visual Studio .NET 2002/2003. It was fixed in Visual Studio
2005.
More Information
Steps to reproduce the problem:
- Create two add-ins with the code below. Each add-in shows a
message when connected and each one should show a message when the user
clicks the Help \ About menu. However, only the last loaded add-in shows the
second message.
| Language: VB.NET | Copy Code (IE only) |
Public Class Connect
Implements Extensibility.IDTExtensibility2
Private m_DTE As EnvDTE.DTE
Private WithEvents m_aboutCommandEvents As EnvDTE.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
m_DTE = CType(application, EnvDTE.DTE)
Select Case connectMode
Case ext_ConnectMode.ext_cm_Startup
' Do nothing until the IDE is fully initialized. OnStartupComplete will be called
Case ext_ConnectMode.ext_cm_AfterStartup
InitializeAddIn()
End Select
End Sub
Public Sub OnStartupComplete(ByRef custom As System.Array) _
Implements Extensibility.IDTExtensibility2.OnStartupComplete
InitializeAddIn()
End Sub
Private Sub InitializeAddIn()
Dim command As EnvDTE.Command
command = m_DTE.Commands.Item("Help.About")
m_aboutCommandEvents = m_DTE.Events.CommandEvents(command.Guid, command.ID)
MessageBox.Show("VB.NET Add-in connected")
End Sub
Private Sub m_objAboutCommandEvents_BeforeExecute(ByVal Guid As String, ByVal ID As Integer, _
ByVal CustomIn As Object, ByVal CustomOut As Object, ByRef CancelDefault As Boolean) _
Handles m_aboutCommandEvents.BeforeExecute
MessageBox.Show("Before showing about window from VB.NET add-in")
End Sub
Public Sub OnDisconnection(ByVal RemoveMode As Extensibility.ext_DisconnectMode, _
ByRef custom As System.Array) _
Implements Extensibility.IDTExtensibility2.OnDisconnection
End Sub
Public Sub OnAddInsUpdate(ByRef custom As System.Array) _
Implements Extensibility.IDTExtensibility2.OnAddInsUpdate
End Sub
Public Sub OnBeginShutdown(ByRef custom As System.Array) _
Implements Extensibility.IDTExtensibility2.OnBeginShutdown
End Sub
End Class
| Language: C# | Copy Code (IE only) |
public class Connect: Extensibility.IDTExtensibility2
{
private EnvDTE.DTE m_DTE;
private EnvDTE.CommandEvents m_aboutCommandEvents;
public void OnConnection(object application, Extensibility.ext_ConnectMode connectMode, object addInInst, ref System.Array custom)
{
m_DTE = (EnvDTE.DTE) application;
switch (connectMode)
{
case ext_ConnectMode.ext_cm_Startup:
// Do nothing until the IDE is fully initialized. OnStartupComplete will be called
break;
case ext_ConnectMode.ext_cm_AfterStartup:
InitializeAddIn();
break;
}
}
public void OnStartupComplete(ref System.Array custom)
{
InitializeAddIn();
}
private void InitializeAddIn()
{
EnvDTE.Command command;
command = m_DTE.Commands.Item("Help.About", -1);
m_aboutCommandEvents = m_DTE.Events.get_CommandEvents(command.Guid, command.ID);
m_aboutCommandEvents.BeforeExecute += new _dispCommandEvents_BeforeExecuteEventHandler(m_aboutCommandEvents_BeforeExecute);
MessageBox.Show("C# Add-in connected");
}
public void OnDisconnection(Extensibility.ext_DisconnectMode disconnectMode, ref System.Array custom)
{
if (m_aboutCommandEvents != null)
{
m_aboutCommandEvents.BeforeExecute -= new _dispCommandEvents_BeforeExecuteEventHandler(m_aboutCommandEvents_BeforeExecute);
}
}
private void m_aboutCommandEvents_BeforeExecute(string Guid, int id, object customIn, object customOut, ref bool cancelDefault)
{
MessageBox.Show("Before showing about window from C# add-in");
}
public void OnAddInsUpdate(ref System.Array custom)
{
}
public void OnBeginShutdown(ref System.Array custom)
{
}
}
Go back to the 'Resources for Visual Studio .NET extensibility' section for more articles like this
You can code, design, locate code and document your apps much faster using VB.NET, C#, C++ or Visual J#:
