BUG: Setting CommandBarPopup.Visible to False doesn't work when CommandBarPopup in context menu.

Author: Carlos J. Quintero (Microsoft MVP) Applies to: Microsoft Visual Studio 2010
Date: August  2012   Microsoft Visual Studio 2012

When an add-in that creates a CommandBarPopup in a context menu (for example, in the code window context menu), if you set the CommandBarPopup.Visible property to false before adding children buttons, the CommandBarPopup remains visible. However, if the CommandBarPopup is on a toolbar, it is hidden as expected. This didn't happen in Visual Studio 2005/2008; it is a bug introduced when VS 2010 migrated from Office-based commandbars to WPF-based commandbars.

As a workaround, you need to add at least one child CommandBarButton to the CommandBarPopup before making it invisible.

More information

Steps to reproduce the problem:

  • Create a VB.NET add-in with the code below.
  • The code creates two CommandbarPopups, one in the standard toolbar and other in the code window context menu, and then makes both invisible.
Language: VB.NET   Copy Code Copy Code (IE only)
Imports System
Imports Microsoft.VisualStudio.CommandBars
Imports Extensibility
Imports EnvDTE
Imports EnvDTE80

Public Class Connect	
   Implements IDTExtensibility2

   Private _applicationObject As DTE2
   Private _addInInstance As AddIn
   Private _commandBarPopup1 As CommandBarPopup
   Private _commandBarPopup2 As CommandBarPopup

   Public Sub OnConnection(ByVal application As Object, ByVal connectMode As ext_ConnectMode, ByVal addInInst As Object, _
      ByRef custom As Array) Implements IDTExtensibility2.OnConnection

      _applicationObject = CType(application, DTE2)
      _addInInstance = CType(addInInst, AddIn)

      Select Case connectMode

         Case ext_ConnectMode.ext_cm_Startup
            ' OnStartupComplete will be called

         Case ext_ConnectMode.ext_cm_AfterStartup

      End Select

   End Sub

   Public Sub OnDisconnection(ByVal disconnectMode As ext_DisconnectMode, ByRef custom As Array) _
      Implements IDTExtensibility2.OnDisconnection

      If _commandBarPopup1 IsNot Nothing Then
      End If

      If _commandBarPopup2 IsNot Nothing Then
      End If

   End Sub

   Public Sub OnAddInsUpdate(ByRef custom As Array) Implements IDTExtensibility2.OnAddInsUpdate
   End Sub

   Public Sub OnStartupComplete(ByRef custom As Array) Implements IDTExtensibility2.OnStartupComplete
   End Sub

   Public Sub OnBeginShutdown(ByRef custom As Array) Implements IDTExtensibility2.OnBeginShutdown
   End Sub

   Private Sub InitializeAddIn()

      Dim commandBars As CommandBars
      Dim codeWindowCommandBar As CommandBar
      Dim standardCommandBar As CommandBar

      commandBars = CType(_applicationObject.CommandBars, CommandBars)

      codeWindowCommandBar = commandBars.Item("Code Window")
      standardCommandBar = commandBars.Item("Standard")

      _commandBarPopup1 = CType(codeWindowCommandBar.Controls.Add(MsoControlType.msoControlPopup), CommandBarPopup)
      _commandBarPopup1.Caption = "My popup 1"
      _commandBarPopup2 = CType(standardCommandBar.Controls.Add(MsoControlType.msoControlPopup), CommandBarPopup)
      _commandBarPopup2.Caption = "My popup 2"

      MessageBox.Show("Now the commandbar popups are going to be invisible")

      _commandBarPopup1.Visible = False
      _commandBarPopup2.Visible = False

   End Sub

End Class

Related articles

Go to the 'Visual Studio Extensibility (VSX)' web site for more articles like this (Articles section)