Partager via


How to: Expose an Add-In on the Tools Menu (Visual Basic)

Visual Studio add-ins are deprecated in Visual Studio 2013. You should upgrade your add-ins to VSPackage extensions. For more information about upgrading, see FAQ: Converting Add-ins to VSPackage Extensions.

When you create an add-in by using the Add-In Wizard and select the option to display it as a command, the command is on the Tools menu by default. If you skip this option when you create the add-in, however, you can simply run the Add-In Wizard again, check that option, and then copy your existing code to the new add-in.

If doing this is not possible, though, the following procedure accomplishes the same thing.

Note

The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. These procedures were developed with the General Development Settings active. To change your settings, choose Import and ExportSettings on the Tools menu. For more information, see Customizing Development Settings in Visual Studio.

To add a menu command to an existing add-in

  1. In the Connect class of the add-in, add Implements IDTCommandTarget.

    This gives you access to the IDTCommandTarget commands interface which is necessary for creating commands.

  2. In the OnConnection procedure, add the following:

    Imports System
    Imports Microsoft.VisualStudio.CommandBars
    Imports Extensibility
    Imports EnvDTE
    Imports EnvDTE80
    
    _applicationObject = CType(application, DTE2)
    _addInInstance = CType(addInInst, AddIn)
    If connectMode = ext_ConnectMode.ext_cm_Startup Then
        Dim commands As Commands2 = CType(_applicationObject.Commands, _
          Commands2)
        Dim toolsMenuName As String
        Try
            Dim resourceManager As System.Resources.ResourceManager = _
            New System.Resources.ResourceManager _
             ("MyAddin1.CommandBar", System.Reflection.Assembly. _
             GetExecutingAssembly())
    
            Dim cultureInfo As System.Globalization.CultureInfo = New _
             System.Globalization.CultureInfo(_applicationObject. _
             LocaleID)
            toolsMenuName = resourceManager.GetString(String.Concat _
              (cultureInfo.TwoLetterISOLanguageName, "Tools"))
    
        Catch e As Exception
            toolsMenuName = "Tools"
        End Try
    
        Dim commandBars As CommandBars = _
          CType(_applicationObject.CommandBars, CommandBars)
        Dim menuBarCommandBar As CommandBar = _
          commandBars.Item("MenuBar")
    
        Dim toolsControl As CommandBarControl = _
          menuBarCommandBar.Controls.Item(toolsMenuName)
        Dim toolsPopup As CommandBarPopup = CType(toolsControl, _
          CommandBarPopup)
    
        Try
            Dim command As Command = _
              commands.AddNamedCommand2(_addInInstance, "MyAddin1", _
              "MyAddin1", "Executes the command for MyAddin1", True, _
              59, Nothing, CType(vsCommandStatus. _
              vsCommandStatusSupported, Integer) + _
              CType(vsCommandStatus.vsCommandStatusEnabled,  _
              Integer), vsCommandStyle.vsCommandStylePictAndText, _
              vsCommandControlType.vsCommandControlTypeButton)
    
           command.AddControl(toolsPopup.CommandBar, 1)
        Catch argumentException As System.ArgumentException
        End Try
    End If
    

    This code executes when the add-in is loaded ("connected") in Visual Studio. It determines if the add-in was loaded with a ext_ConnectMode value of ext_cm_UISetup. This means the add-in was started for the first time since being installed. If this is true, then a command is created for it on the Tools menu by using the AddNamedCommand method. For more information, see How to: Add and Handle Commands.

  3. Add the following two procedures to the Connect class.

    The QueryStatus method is called when the command's availability is updated. The Exec method is called when the command is invoked.

    Public Sub QueryStatus(ByVal commandName As String,  _
      ByVal neededText As vsCommandStatusTextWanted, ByRef status As _
      vsCommandStatus, ByRef commandText As Object) Implements _
      IDTCommandTarget.QueryStatus
        If neededText = vsCommandStatusTextWanted. _
          vsCommandStatusTextWantedNone Then
            If commandName = "MyAddin1.Connect.MyAddin1" Then
                status = CType(vsCommandStatus.vsCommandStatusEnabled _
                  + vsCommandStatus.vsCommandStatusSupported, _
                  vsCommandStatus)
            Else
                status = vsCommandStatus.vsCommandStatusUnsupported
            End If
        End If
    End Sub
    
    Public Sub Exec(ByVal commandName 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 commandName = "MyAddin1.Connect.MyAddin1" Then
                handled = True
                Exit Sub
            End If
        End If
    End Sub
    

    Each time you implement IDTCommandTarget, you must add these two procedures. A quick way to do it is to select IDTCommandTarget in the Class Name drop-down box at the top-left corner of the editor. Select each procedure in turn from the Method Name drop-down box in the top-right corner. This creates the necessary empty procedures with the correct parameters to which you can then add code.

    The Exec procedure is called when a user clicks your menu command, so insert code there that you want to execute at that time.

See Also

Tasks

How to: Expose an Add-In on the Tools Menu (Visual C#)

How to: Control Add-Ins By Using the Add-In Manager

How to: Create an Add-In

Concepts

Automation Object Model Chart