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

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 produces the same result.

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. Add these using statements to the file containing the Connect class.

    using Microsoft.VisualStudio.CommandBars;
    using System.Resources;
    using System.Reflection;
    using System.Globalization;
    using System.Windows.Forms;
    
  2. Change the Connect class declaration to implement IDTCommandTarget.

  3. Replace or change the OnConnection() procedure code to the following:

    public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
    {
        _applicationObject = (DTE2)application;
        _addInInstance = (AddIn)addInInst;
        if(connectMode == ext_ConnectMode.ext_cm_UISetup)
        {
            object []contextGUIDS = new object[] { };
            Commands2 commands = (Commands2)_applicationObject.Commands;
            string toolsMenuName;
    
            try
            {
                ResourceManager resourceManager = new     
                  ResourceManager("MyAddin1.CommandBar",  
                  Assembly.GetExecutingAssembly());
                CultureInfo cultureInfo = new 
                  System.Globalization.CultureInfo
                  (_applicationObject.LocaleID);
                string resourceName = String.Concat(cultureInfo.
                  TwoLetterISOLanguageName, "Tools");
                toolsMenuName = resourceManager.GetString(resourceName);
            }
            catch
            {
                toolsMenuName = "Tools";
            }
    
            CommandBar menuBarCommandBar = 
              ((CommandBars)_applicationObject.CommandBars)
              ["MenuBar"];
    
              CommandBarControl toolsControl = 
                menuBarCommandBar.Controls[toolsMenuName];
              CommandBarPopup toolsPopup = 
                (CommandBarPopup)toolsControl;
    
              try
              {
                  Command command = commands.AddNamedCommand2
                    (_addInInstance, "MyAddin1", "MyAddin1", "Executes  
                    the command for MyAddin1", true, 59, ref 
                    contextGUIDS, (int)vsCommandStatus.
                    vsCommandStatusSupported+(int)vsCommandStatus.
                    vsCommandStatusEnabled, (int)vsCommandStyle.
                    vsCommandStylePictAndText, vsCommandControlType.
                    vsCommandControlTypeButton);
    
                  if((command != null) && (toolsPopup != null))
                  {
                         command.AddControl(toolsPopup.CommandBar, 1);
                  }
            }
            catch(System.ArgumentException)
            {
            }
        }
    }
    
  4. Add the following two required procedures, QueryStatus and Exec:

    public void QueryStatus(string commandName, 
      vsCommandStatusTextWanted neededText, ref vsCommandStatus status, 
      ref object commandText)
    {
        if(neededText == 
          vsCommandStatusTextWanted.vsCommandStatusTextWantedNone)
        {
            if(commandName == "MyAddin1.Connect.MyAddin1")
            {
                status = (vsCommandStatus)vsCommandStatus.
                  vsCommandStatusSupported|vsCommandStatus.
                  vsCommandStatusEnabled;
                return;
            }
        }
    }
    
    public void Exec(string commandName, vsCommandExecOption 
      executeOption, ref object varIn, ref object varOut, ref bool 
      handled)
    {
        handled = false;
        if(executeOption ==  
          vsCommandExecOption.vsCommandExecOptionDoDefault)
        {
            if(commandName == "MyAddin1.Connect.MyAddin1")
            {
                handled = true;
                    System.Windows.Forms.MessageBox.
                      Show("add-in running.");
                return;
            }
        }
    }
    

    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 Basic)

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

How to: Create an Add-In

Concepts

Automation Object Model Chart