Ver versión en español    
 
Home
10 Reasons to use MZ-Tools
MZ-Tools 6.0 for VS.NET
Editions
Features
Online Documentation
MZ-Tools SDK
Download
Purchase
Version History (RSS)  
FAQ & Support
MZ-Tools 3.0 for VB6 & VBA
Features
Online Documentation
Download (freeware)
Donations (Paypal)
Version History (RSS)  
FAQ & Support
User Reviews
Community Place
Contact  
For Add-In Developers
About
   
 
User Testimonials

I'm an avid supporter of MZ-Tools. It's a product I couldn't do without and your level of support is outstanding.

Jan Hyde (Visual Basic MVP)

You will soon wonder how you ever lived without it.

Andy Maggs

More user reviews
 
HOWTO: Adding buttons, commandbars and toolbars to Visual Studio .NET from an add-in

Author: Carlos J. Quintero (Microsoft MVP) Applies to: Microsoft Visual Studio .NET 2002
Date: June 2005   Microsoft Visual Studio .NET 2003
Updated: May 2007   Microsoft Visual Studio 2005
       
Introduction

This article provides a code sample to add buttons, commandbars and toolbars to the Visual Studio .NET IDE from an add-in.

More information

Visual Studio .NET allows add-ins to add commandbars of two kinds, permanent or temporary:

  • Temporary commandbars: the add-in creates these commandbars each time that it is loaded and removes them each time it is unloaded.

    To create a temporary commandbar:
    • Create it each time that the add-in is loaded, when the OnConnection method receives the values ext_ConnectMode.ext_cm_AfterStartup or ext_ConnectMode.ext_cm_Startup (but not when it receives the value ext_ConnectMode.ext_cm_UISetup).
    • Use the DTE.CommandBars.Add method for toolbars or the CommandBar.Controls.Add method for a commandbar inside other commandbar.

    To remove a temporary commandbar:
    • Remove it when the add-in is unloaded.
    • Use the CommandBar.Delete method.
  • Permanent commandbars: these commandbars are always visible in the IDE, even if the add-in is unloaded through the Add-in Manager. The rationale for this behavior is to optimize the startup of the IDE and add-ins: the commandbars are created faster if they are persisted and retrieved from disk by Visual Studio than if an add-in have to create them and add all its buttons by code when the add-in is loaded.

    To create a permanent commandbar:
    • Create it only once, when the OnConnection method receives the value ext_ConnectMode.ext_cm_UISetup in the connectMode parameter, which happens only once after an add-in is installed on a machine (more on this later).
    • Use the DTE.Commands.AddCommandBar method.

    To remove a permanent commandbar:

    Since permanent commandbars remain visible in the IDE even if the add-in is unloaded through the Add-in Manager, this behavior will be confusing for many users and most add-ins use temporary commandbars instead. Also, permanent commandbars are complicated during development, because for their own nature they are not removed, and you can get duplicated commandbars if a new ext_ConnectMode.ext_cm_UISetup phase is executed (specially in Visual Studio 2005; more on this later). It is recomended that you use temporary commandbars.

Important notes

  • You should only use DTE.Commands.AddCommandBar to create permanent commandbars when connectMode has the value ext_ConnectMode.ext_cm_UISetup, and you should only use DTE.CommandBars.Add to create temporary commandbars when connectMode is ext_ConnectMode.ext_cm_AfterStartup or ext_ConnectMode.ext_cm_Startup. Do not mix both models.
  • In order to receive the ext_ConnectMode.ext_cm_UISetup flag in the OnConnection method, your add-in registration must specify that it wants "CommandPreload" (see HOWTO: Reset a Visual Studio add-in). When you use the add-in wizard to create an add-in project, you must check the option "Yes, create a "Tools" menu item. By default this will cause the Add-in to load when the button is clicked unless the Add-in is set to load on startup of the host application.". If you don't check that option and later you change your mind and want a ext_ConnectMode.ext_cm_UISetup phase, you need to change the Windows registry setting or XML tag value specified in that article.
  • Notice that ext_ConnectMode.ext_cm_UISetup is fired only once in the whole life of an add-in on the computer, that is, it is not fired each time that the add-in is loaded (keep on reading some bullets if you think otherwise because on your machine it is fired always when you debug your add-in in Visual Studio 2005 or higher). The flag ext_ConnectMode.ext_cm_UISetup is intended to create permanent user interface only once and forever.
  • If, while developing your add-in, you want to add new permanent user interface, in order to have a new ext_ConnectMode.ext_cm_UISetup phase to add it, you would need to reset some registry flags (see HOWTO: Reset a Visual Studio add-in).
  • Visual Studio 2005 introduced a handy new command-line flag to reset those registry entries to get a new ext_ConnectMode.ext_cm_UISetup phase:

    devenv.exe /resetaddin <AddInNamespace.Connect>
     
  • The add-in project generated by the add-in wizard of Visual Studio 2005 (on the contrary to VS.NET 2002/2003) adds that command-line flag to devenv.exe in the property pages of the add-in project ("Debug" tab, "Start Options" section, "Command line arguments" field) to reset the add-in each time you debug it. This could lead to the false conclusion that ext_ConnectMode.ext_cm_UISetup is fired each time that the add-in runs. It is not. It only happens when you debug add-ins with Visual Studio 2005 or higher. It does not happen with Visual Studio .NET 2003, it does not happen when the add-in is used on a final machine (without debugging) and if you remove the /resetaddin flag of the property page of your Visual Studio 2005 project, it won't happen too when you debug your add-in.
  • The /resetaddin command-line flag of Visual Studio 2005 or higher removes commands and permanent buttons created by the add-in, but not permanent commandbars. So, you will get duplicated permanent commandbars in Visual Studio 2005 or higher each time that you debug your add-in, unless your code deletes a previous commandbar before adding it again.
  • Commands should be created only once and forever, and removed only when the add-in is uninstalled, rather than creating them when the add-in is loaded and removed when the add-in is unloaded. The reason for this is that if the user has associated keyboard shortcuts to them, they would be lost on each session. Therefore, commands can be created either:
    • When the connectMode has the value ext_ConnectMode.ext_cm_UISetup, which will happen only once in the whole life of the add-in on the computer.
    • When connectMode is ext_ConnectMode.ext_cm_AfterStartup or ext_ConnectMode.ext_cm_Startup if you detect that they do not exist yet. This approach is more robust (see INFO: Visual Studio .NET Add-In Commands Disappear On Next Session) at the cost of being somewhat more time-consuming. This approach will be used in the code of this article.

    In any case, controls (buttons) are added to a CommandBar using Command.AddControl(commandBar). This function returns a CommandBarControl, which has properties to set the Caption, etc. Some properties, though, will require to cast it to a CommandBarButton, for example to set the BeginGroup or the Style (msoButtonIcon, msoButtonIconAndCaption, etc.) properties. The status of a CommandBarControl (visible, enabled, etc.) is controlled through its underlying Command, in the implementation of the IDTCommandTarget.QueryStatus method, not through the properties of CommandBarControl.
  • There are a couple of bugs in the Visual Studio .NET 2002/2003 IDE that cause a reload of an add-in after being unloaded if it has added a CommandBarControl to some built-in commandbar of the IDE (such as the "Tools" menu) and another add-ins has done the same, or if it has created a toolbar which is on the same row that the toolbar of other add-in.

Sample code

The following source code (VB.NET) shows an add-in that creates an EnvDTE.Command and exposes it in the Visual Studio .NET IDE through the following UI elements:

  • A button on the "Standard" toolbar of Visual Studio .NET .
  • A menu entry on the "Tools" menu of Visual Studio .NET .
  • A menu entry on the context menu of a code window.
  • A new menu on the main menu of Visual Studio .NET .
  • A new toolbar.

Both permanent and temporary approaches are shown.

For temporary commandbars:

Public Class Connect
   Implements Extensibility.IDTExtensibility2
   Implements IDTCommandTarget
   Private Const MY_COMMAND_NAME As String = "MyCommand"
   Private applicationObject As EnvDTE.DTE
   Private addInInstance As EnvDTE.AddIn
   Private myStandardCommandBarControl As CommandBarControl
   Private myToolsCommandBarControl As CommandBarControl
   Private myCodeWindowCommandBarControl As CommandBarControl
   Private myTemporaryToolbar As CommandBar
   Private myTemporaryCommandBarPopup As CommandBarPopup
   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
      Try
         applicationObject = CType(application, EnvDTE.DTE)
         addInInstance = CType(addInInst, EnvDTE.AddIn)
         Select Case connectMode
            Case ext_ConnectMode.ext_cm_Startup
               ' The add-in was marked to load on startup
               ' Do nothing at this point because the IDE may not be fully initialized
               ' VS will call OnStartupComplete when ready
            Case ext_ConnectMode.ext_cm_AfterStartup
               ' The add-in was loaded after startup
               ' Initialize it in the same way that when is loaded on startup calling manually this method:
               OnStartupComplete(Nothing)
         End Select
      Catch e As System.Exception
         System.Windows.Forms.MessageBox.Show(e.ToString)
      End Try
   End Sub
   Public Sub OnStartupComplete(ByRef custom As System.Array) _
      Implements Extensibility.IDTExtensibility2.OnStartupComplete
       Dim myCommand As Command = Nothing
      Dim standardCommandBar As CommandBar
      Dim menuCommandBar As CommandBar
      Dim toolsCommandBar As CommandBar
      Dim codeCommandBar As CommandBar
      Dim toolsCommandBarControl As CommandBarControl
      Dim myCommandBarButton As CommandBarButton
      Dim position As Integer
      Dim commandBars As CommandBars
      Try

         ' Try to retrieve the command, just in case it was already created
         Try
            myCommand = applicationObject.Commands.Item(addInInstance.ProgID & _
               "." & MY_COMMAND_NAME)
         Catch
         End Try
         ' Add the command if it does not exist
         If myCommand Is Nothing Then
            myCommand = applicationObject.Commands.AddNamedCommand(addInInstance, _
               MY_COMMAND_NAME, MY_COMMAND_NAME, "Executes the command for MyAddin", True, _
               59, Nothing, vsCommandStatus.vsCommandStatusSupported Or _
               vsCommandStatus.vsCommandStatusEnabled)
         End If
         ' Retrieve the collection of commandbars
         ' - In VS.NET 2002/2003 (which uses the Office.dll reference) 
         '   DTE.CommandBars returns directly a CommandBars type, so a cast 
         '   to CommandBars is redundant
         ' - In VS 2005 (which uses the new Microsoft.VisualStudio.CommandBars.dll reference) 
         '   DTE.CommandBars returns an Object type, so we do need a cast to CommandBars
         commandBars = DirectCast(applicationObject.CommandBars, CommandBars)
         ' Retrieve some built-in command bars
         standardCommandBar = commandBars.Item("Standard")
         menuCommandBar = commandBars.Item("MenuBar")
         toolsCommandBar = commandBars.Item("Tools")
         codeCommandBar = commandBars.Item("Code Window")
         ' Create the buttons from the commands
         ' Note:
         ' - In VS.NET 2002/2003 (which uses the Office.dll reference) 
         '   Command.AddControl returns directly a CommandBarControl type, so a cast 
         '   to CommandBarControl is redundant
         ' - In VS 2005 (which uses the new Microsoft.VisualStudio.CommandBars.dll reference) 
         '   Command.AddControl returns an Object type, so we do need a cast to CommandBarControl
         ' Add a button to the built-in "Standard" toolbar
         myStandardCommandBarControl = DirectCast(myCommand.AddControl(standardCommandBar, _
            standardCommandBar.Controls.Count + 1), CommandBarControl)
         myStandardCommandBarControl.Caption = MY_COMMAND_NAME
         ' Change the button style, which must be done casting the control to a button
         myCommandBarButton = DirectCast(myStandardCommandBarControl, CommandBarButton)
         myCommandBarButton.Style = MsoButtonStyle.msoButtonIcon
         ' Add a button to the built-in "Tools" menu
         myToolsCommandBarControl = DirectCast(myCommand.AddControl(toolsCommandBar, _
            toolsCommandBar.Controls.Count + 1), CommandBarControl)
         myToolsCommandBarControl.Caption = MY_COMMAND_NAME
         ' Add a button to the built-in "Code Window" context menu
         myCodeWindowCommandBarControl = DirectCast(myCommand.AddControl(codeCommandBar, _
            codeCommandBar.Controls.Count + 1), CommandBarControl)
         myCodeWindowCommandBarControl.Caption = MY_COMMAND_NAME
         ' Add a new toolbar with a button on it
         myTemporaryToolbar = commandBars.Add("MyTemporaryToolbar", _
            MsoBarPosition.msoBarTop, System.Type.Missing, True)
         ' Change the button style, which must be done casting the control to a button
         myCommandBarButton = DirectCast(myCommand.AddControl(myTemporaryToolbar), _
            CommandBarButton)
         myCommandBarButton.Style = MsoButtonStyle.msoButtonIcon
         ' Make visible the toolbar
         myTemporaryToolbar.Visible = True
         ' Calculate the position of a new command bar popup by the "Tools" menu
         toolsCommandBarControl = DirectCast(toolsCommandBar.Parent, CommandBarControl)
         position = toolsCommandBarControl.Index + 1
         ' Add a new command bar popup with a button on it
         myTemporaryCommandBarPopup = DirectCast(menuCommandBar.Controls.Add( _
            MsoControlType.msoControlPopup, System.Type.Missing, System.Type.Missing, _
            position, True), CommandBarPopup)
         myTemporaryCommandBarPopup.CommandBar.Name = "MyTemporaryCommandBarPopup"
         myTemporaryCommandBarPopup.Caption = "My menu"
         myCommand.AddControl(myTemporaryCommandBarPopup.CommandBar)
         myTemporaryCommandBarPopup.Visible = True
      Catch e As System.Exception
         System.Windows.Forms.MessageBox.Show(e.ToString)
      End Try
   End Sub
   Public Sub OnDisconnection(ByVal RemoveMode As Extensibility.ext_DisconnectMode, _
      ByRef custom As System.Array) _
      Implements Extensibility.IDTExtensibility2.OnDisconnection
      Try
         If Not (myStandardCommandBarControl Is Nothing) Then
            myStandardCommandBarControl.Delete()
         End If
         If Not (myCodeWindowCommandBarControl Is Nothing) Then
            myCodeWindowCommandBarControl.Delete()
         End If
         If Not (myToolsCommandBarControl Is Nothing) Then
            myToolsCommandBarControl.Delete()
         End If
         If Not (myTemporaryToolbar Is Nothing) Then
            myTemporaryToolbar.Delete()
         End If
         If Not (myTemporaryCommandBarPopup Is Nothing) Then
            myTemporaryCommandBarPopup.Delete()
         End If
      Catch e As System.Exception
         System.Windows.Forms.MessageBox.Show(e.ToString)
      End Try
   End Sub
   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 Exec(ByVal cmdName As String, _
      ByVal executeOption As vsCommandExecOption, _
      ByRef varIn As Object, ByRef varOut As Object, ByRef handled As Boolean) _
      Implements IDTCommandTarget.Exec
      handled = False
      If (executeOption = vsCommandExecOption.vsCommandExecOptionDoDefault) Then
         If cmdName = addInInstance.ProgID & "." & MY_COMMAND_NAME Then
            handled = True
            System.Windows.Forms.MessageBox.Show("Command executed.")
         End If
      End If
   End Sub
   Public Sub QueryStatus(ByVal cmdName As String, _
      ByVal neededText As vsCommandStatusTextWanted, _
      ByRef statusOption As vsCommandStatus, ByRef commandText As Object) _
      Implements IDTCommandTarget.QueryStatus
      If neededText = vsCommandStatusTextWanted.vsCommandStatusTextWantedNone Then
         If cmdName = addInInstance.ProgID & "." & MY_COMMAND_NAME Then
            statusOption = CType(vsCommandStatus.vsCommandStatusEnabled + _
               vsCommandStatus.vsCommandStatusSupported, vsCommandStatus)
         Else
            statusOption = vsCommandStatus.vsCommandStatusUnsupported
         End If
      End If
   End Sub

End Class

For permanent commandbars:

Public Class Connect
    Implements Extensibility.IDTExtensibility2
    Implements IDTCommandTarget
    Private Const MY_COMMAND_NAME As String = "MyCommand"
    Private applicationObject As EnvDTE.DTE
    Private addInInstance As EnvDTE.AddIn
    Private myStandardCommandBarControl As CommandBarControl
    Private myToolsCommandBarControl As CommandBarControl
    Private myCodeWindowCommandBarControl As CommandBarControl
    Private myPermanentToolbar As CommandBar
    Private myPermanentCommandBarPopup As CommandBar
    Private myPermanentCommandBarMenu As CommandBar
    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
        Const MY_PERMANENT_MENU As String = "My Permanent Menu"
        Const MY_PERMANENT_SUBMENU As String = "My Permanent Submenu"
        Const MY_PERMANENT_TOOLBAR As String = "My Permanent Toolbar"
        Dim myCommand As Command = Nothing
        Dim standardCommandBar As CommandBar
        Dim menuCommandBar As CommandBar
        Dim toolsCommandBar As CommandBar
        Dim codeCommandBar As CommandBar
        Dim toolsCommandBarControl As CommandBarControl
        Dim myCommandBarButton As CommandBarButton
        Dim position As Integer
        Dim commandBars As CommandBars
        Try
            applicationObject = CType(application, EnvDTE.DTE)
            addInInstance = CType(addInInst, EnvDTE.AddIn)
            Select Case connectMode
                Case ext_ConnectMode.ext_cm_UISetup
                    myCommand = applicationObject.Commands.AddNamedCommand(addInInstance, _
                       MY_COMMAND_NAME, MY_COMMAND_NAME, "Executes the command for MyAddin", True, _
                       59, Nothing, vsCommandStatus.vsCommandStatusSupported Or _
                       vsCommandStatus.vsCommandStatusEnabled)
                    ' Retrieve the collection of commandbars
                    ' - In VS.NET 2002/2003 (which uses the Office.dll reference) 
                    '   DTE.CommandBars returns directly a CommandBars type, so a cast 
                    '   to CommandBars is redundant
                    ' - In VS 2005 (which uses the new Microsoft.VisualStudio.CommandBars.dll reference) 
                    '   DTE.CommandBars returns an Object type, so we do need a cast to CommandBars
                    commandBars = DirectCast(applicationObject.CommandBars, CommandBars)
                    ' Retrieve some built-in command bars
                    standardCommandBar = commandBars.Item("Standard")
                    menuCommandBar = commandBars.Item("MenuBar")
                    toolsCommandBar = commandBars.Item("Tools")
                    codeCommandBar = commandBars.Item("Code Window")
                    ' Create the buttons from the commands
                    ' Note:
                    ' - In VS.NET 2002/2003 (which uses the Office.dll reference) 
                    '   Command.AddControl returns directly a CommandBarControl type, so a cast 
                    '   to CommandBarControl is redundant
                    ' - In VS 2005 (which uses the new Microsoft.VisualStudio.CommandBars.dll reference) 
                    '   Command.AddControl returns an Object type, so we do need a cast to CommandBarControl
                    ' Add a button to the built-in "Standard" toolbar
                    myStandardCommandBarControl = DirectCast(myCommand.AddControl(standardCommandBar, _
                       standardCommandBar.Controls.Count + 1), CommandBarControl)
                    myStandardCommandBarControl.Caption = MY_COMMAND_NAME
                    ' Change the button style, which must be done casting the control to a button
                    myCommandBarButton = DirectCast(myStandardCommandBarControl, CommandBarButton)
                    myCommandBarButton.Style = MsoButtonStyle.msoButtonIcon
                    ' Add a button to the built-in "Tools" menu
                    myToolsCommandBarControl = DirectCast(myCommand.AddControl(toolsCommandBar, _
                       toolsCommandBar.Controls.Count + 1), CommandBarControl)
                    myToolsCommandBarControl.Caption = MY_COMMAND_NAME
                    ' Add a button to the built-in "Code Window" context menu
                    myCodeWindowCommandBarControl = DirectCast(myCommand.AddControl(codeCommandBar, _
                       codeCommandBar.Controls.Count + 1), CommandBarControl)
                    myCodeWindowCommandBarControl.Caption = MY_COMMAND_NAME
                    ' Add a new toolbar with a button on it, trying to delete it if it exists from a 
                    ' previous execution, because the /resetaddin command-line switch of VS 2005 add-in 
                    ' projects only resets commands and buttons, not commandbars
                    Try
                        commandBars.Item(MY_PERMANENT_TOOLBAR).Delete()
                    Catch ex As Exception
                    End Try
                    myPermanentToolbar = DirectCast(applicationObject.Commands.AddCommandBar( _
                       MY_PERMANENT_TOOLBAR, vsCommandBarType.vsCommandBarTypeToolbar, Nothing, 1), _
                       CommandBar)
                    myPermanentToolbar.Position = MsoBarPosition.msoBarTop
                    myPermanentToolbar.RowIndex = 10 ' Hopefully the last one
                    ' Make the toolbar visible
                    myPermanentToolbar.Visible = True
                    ' Change the button style, which must be done casting the control to a button
                    myCommandBarButton = DirectCast(myCommand.AddControl(myPermanentToolbar), _
                       CommandBarButton)
                    myCommandBarButton.Style = MsoButtonStyle.msoButtonIcon
                    ' Calculate the position of a new command bar popup by the "Tools" menu
                    toolsCommandBarControl = DirectCast(toolsCommandBar.Parent, CommandBarControl)
                    position = toolsCommandBarControl.Index + 1
                    ' Add a new command bar menu in the main menu with a button on it
                    Try
                        commandBars.Item(MY_PERMANENT_MENU).Delete()
                        menuCommandBar.Controls.Item(MY_PERMANENT_MENU).Delete()
                    Catch ex As Exception
                    End Try
                    myPermanentCommandBarMenu = DirectCast(applicationObject.Commands.AddCommandBar( _
                      MY_PERMANENT_MENU, vsCommandBarType.vsCommandBarTypeMenu, menuCommandBar, position), _
                     CommandBar)
                    myCommand.AddControl(myPermanentCommandBarMenu)
                    ' Add a new command bar submenu under the "Tools" menu with a button on it
                    Try
                        commandBars.Item(MY_PERMANENT_SUBMENU).Delete()
                        toolsCommandBar.Controls.Item(MY_PERMANENT_SUBMENU).Delete()
                    Catch ex As Exception
                    End Try
                    myPermanentCommandBarPopup = DirectCast(applicationObject.Commands.AddCommandBar( _
                       MY_PERMANENT_SUBMENU, vsCommandBarType.vsCommandBarTypeMenu, _
                       toolsCommandBar, 1), CommandBar)
                    myCommand.AddControl(myPermanentCommandBarPopup)
            End Select
        Catch e As System.Exception
            System.Windows.Forms.MessageBox.Show(e.ToString)
        End Try
    End Sub
    Public Sub OnDisconnection(ByVal RemoveMode As Extensibility.ext_DisconnectMode, _
       ByRef custom As System.Array) _
       Implements Extensibility.IDTExtensibility2.OnDisconnection
    End Sub
    Public Sub OnStartupComplete(ByRef custom As System.Array) _
       Implements Extensibility.IDTExtensibility2.OnStartupComplete
    End Sub
    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 Exec(ByVal cmdName As String, _
       ByVal executeOption As vsCommandExecOption, _
       ByRef varIn As Object, ByRef varOut As Object, ByRef handled As Boolean) _
       Implements IDTCommandTarget.Exec
        handled = False
        If (executeOption = vsCommandExecOption.vsCommandExecOptionDoDefault) Then
            If cmdName = addInInstance.ProgID & "." & MY_COMMAND_NAME Then
                handled = True
                System.Windows.Forms.MessageBox.Show("Command executed.")
            End If
        End If
    End Sub
    Public Sub QueryStatus(ByVal cmdName As String, _
       ByVal neededText As vsCommandStatusTextWanted, _
       ByRef statusOption As vsCommandStatus, ByRef commandText As Object) _
       Implements IDTCommandTarget.QueryStatus
        If neededText = vsCommandStatusTextWanted.vsCommandStatusTextWantedNone Then
            If cmdName = addInInstance.ProgID & "." & MY_COMMAND_NAME Then
                statusOption = CType(vsCommandStatus.vsCommandStatusEnabled + _
                   vsCommandStatus.vsCommandStatusSupported, vsCommandStatus)
            Else
                statusOption = vsCommandStatus.vsCommandStatusUnsupported
            End If
        End If
    End Sub
End Class

You may have noticed that when calling Commands.AddCommandBar the code uses the value vsCommandBarType.vsCommandBarTypeToolbar for toolbars and the value vsCommandBarType.vsCommandBarTypeMenu for both menu entries in the main menu or in a commandbar such as the "Tools" menu. There is a third value vsCommandBarType.vsCommandBarTypePopup that it is not used in the code. This value is used to create a commandbar popup that appears only when you right-click somewhere (like a context menu). To do this, you would call the ShowPopup(x,y) method of the returned CommandBar.

Related articles

Go back to the 'Resources for Visual Studio .NET extensibility' section for more articles like this

MZ-Tools 6.0 for Visual Studio .NET

You can code, design, locate code and document your apps much faster using VB.NET, C#, C++ or Visual J#:

Buy MZ-Tools Now Download MZ-Tools Demo

   Top