Logo
HOWTO: Set the default style a CommandBarButton on a toolbar from a Visual Studio add-in.

Author: Carlos J. Quintero (Microsoft MVP) Applies to: Microsoft Visual Studio 2005
Date: March 2012   Microsoft Visual Studio 2008
Updated: March 2013   Microsoft Visual Studio 2010
      Microsoft Visual Studio 2012
Introduction

When a CommandBarButton is created from a Command on a Visual Studio menu, it has an icon and a text. However, when it is created on a toolbar, it can have only an icon, or an icon and a text. Typically buttons on a toolbar have only an icon (without a text). There are two ways to set that style (only the icon) to a CommandBarButton on a toolbar:

  • Setting the CommandBarButton.Style = MsoButtonStyle.msoButtonIcon. This approach has the drawback that Visual Studio first creates the button with icon and text, and then it changes it to icon only.
  • Setting the default style of a command to EnvDTE80.vsCommandStyle.vsCommandStylePict, so that CommandBarButtons created on toolbars have only the icon, while those created on menus have icon with text (the only possible style for them).
More information

The following sample shows an add-in that uses the EnvDTE80.Commands2.AddNamedCommand2 method to create a Command with the EnvDTE80.vsCommandStyle.vsCommandStylePict style and a button on the "Standard" toolbar with only the icon, without the need to change the CommandBarButton.Style property to MsoButtonStyle.msoButtonIcon.

Language: VB.NET   Copy Code Copy Code (IE only)
Imports System
Imports Microsoft.VisualStudio.CommandBars
Imports Extensibility
Imports EnvDTE

Public Class Connect
   Implements Extensibility.IDTExtensibility2
   Implements IDTCommandTarget

   Private Const MY_COMMAND_NAME As String = "MyCommand"
   Private Const MY_COMMAND_CAPTION As String = "My command"
   Private Const MY_COMMAND_TOOLTIP As String = "My command tooltip"

   Private applicationObject As EnvDTE.DTE
   Private addInInstance As EnvDTE.AddIn

   Private myStandardCommandBarButton As CommandBarButton

   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_UISetup

               ' Do nothing for this add-in with temporary user interface

            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
               ' Visual Studio will call OnStartupComplete when fully initialized

            Case ext_ConnectMode.ext_cm_AfterStartup

               ' The add-in was loaded by hand after startup using the Add-In Manager
               ' Initialize it in the same way that when is loaded on startup
               AddTemporaryUI()

         End Select

      Catch ex As System.Exception
         System.Windows.Forms.MessageBox.Show(ex.ToString)
      End Try

   End Sub

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

      AddTemporaryUI()

   End Sub

   Private Sub AddTemporaryUI()

      Const VS_STANDARD_COMMANDBAR_NAME As String = "Standard"

      Dim myCommand As Command = Nothing
      Dim standardCommandBar As CommandBar
      Dim commandBars As CommandBars
      Dim commands2 As EnvDTE80.Commands2

      Try

         commands2 = DirectCast(applicationObject.Commands, EnvDTE80.Commands2)

         ' Try to get the command if already exists
         Try
            myCommand = commands2.Item(addInInstance.ProgID & "." & MY_COMMAND_NAME)
         Catch
         End Try

         ' Add the command if it does not exist
         If myCommand Is Nothing Then

            myCommand = commands2.AddNamedCommand2(addInInstance, MY_COMMAND_NAME, _
               MY_COMMAND_CAPTION, MY_COMMAND_TOOLTIP, True, 59, Nothing, _
               vsCommandStatus.vsCommandStatusSupported Or vsCommandStatus.vsCommandStatusEnabled,
               EnvDTE80.vsCommandStyle.vsCommandStylePict)

         End If

         ' Get the "Standard" commmandbar
         commandBars = DirectCast(applicationObject.CommandBars, CommandBars)
         standardCommandBar = commandBars.Item(VS_STANDARD_COMMANDBAR_NAME)

         ' Add a button to the commandbar
         myStandardCommandBarButton = DirectCast(myCommand.AddControl(standardCommandBar, _
            standardCommandBar.Controls.Count + 1), CommandBarButton)

         ' Set the properties of the button
         myStandardCommandBarButton.Caption = MY_COMMAND_CAPTION
         myStandardCommandBarButton.BeginGroup = True

         ' There is no need to set this property
         'myStandardCommandBarButton.Style = MsoButtonStyle.msoButtonIcon

      Catch ex As System.Exception
         System.Windows.Forms.MessageBox.Show(ex.ToString)
      End Try

   End Sub

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

      Try

         Select Case RemoveMode

            Case ext_DisconnectMode.ext_dm_HostShutdown, ext_DisconnectMode.ext_dm_UserClosed

               If Not (myStandardCommandBarButton Is Nothing) Then
                  myStandardCommandBarButton.Delete()
               End If

         End Select

      Catch ex As System.Exception
         System.Windows.Forms.MessageBox.Show(ex.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
Language: C#   Copy Code Copy Code (IE only)
using System;
using Extensibility;
using EnvDTE;
using Microsoft.VisualStudio.CommandBars;
using System.Windows.Forms;

namespace MyAddin
{
   public class Connect : Extensibility.IDTExtensibility2, IDTCommandTarget
   {
      private const string MY_COMMAND_NAME = "MyCommand";
      private const string MY_COMMAND_CAPTION = "My command";
      private const string MY_COMMAND_TOOLTIP = "My command tooltip";

      private EnvDTE.DTE applicationObject;
      private EnvDTE.AddIn addInInstance;

      private CommandBarButton myStandardCommandBarButton;

      public void OnConnection(object application, Extensibility.ext_ConnectMode connectMode,
         object addInInst, ref System.Array custom)
      {
         try
         {
            applicationObject = (EnvDTE.DTE)application;
            addInInstance = (EnvDTE.AddIn)addInInst;

            switch (connectMode)
            {
               case ext_ConnectMode.ext_cm_UISetup:

                  // Do nothing for this add-in with temporary user interface
                  break;

               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
                  // Visual Studio will call OnStartupComplete when fully initialized
                  break;

               case ext_ConnectMode.ext_cm_AfterStartup:

                  // The add-in was loaded by hand after startup using the Add-In Manager
                  // Initialize it in the same way that when is loaded on startup
                  AddTemporaryUI();
                  break;
            }
         }
         catch (System.Exception ex)
         {
            System.Windows.Forms.MessageBox.Show(ex.ToString());
         }
      }

      public void OnStartupComplete(ref System.Array custom)
      {
         AddTemporaryUI();
      }

      private void AddTemporaryUI()
      {
         const string VS_STANDARD_COMMANDBAR_NAME = "Standard";

         Command myCommand = null;
         CommandBar standardCommandBar = null;
         CommandBars commandBars = null;
         EnvDTE80.Commands2 commands2;
         object[] contextUIGuids = new object[] { };
        
         try
         {

            commands2 = (EnvDTE80.Commands2)applicationObject.Commands;

            // Try to get the command if already exists 
            try
            {
               myCommand = commands2.Item(addInInstance.ProgID + "." + MY_COMMAND_NAME, -1);
            }
            catch
            {
            }

            // Add the command if it does not exist
            if (myCommand == null)
            {
               myCommand = commands2.AddNamedCommand2(addInInstance,
                  MY_COMMAND_NAME, MY_COMMAND_CAPTION, MY_COMMAND_TOOLTIP, true, 59, ref contextUIGuids,
                  (int)(vsCommandStatus.vsCommandStatusSupported | vsCommandStatus.vsCommandStatusEnabled),
                  (int)EnvDTE80.vsCommandStyle.vsCommandStylePict, EnvDTE80.vsCommandControlType.vsCommandControlTypeButton);
            }

            // Get the "Standard" commandbar
            commandBars = (CommandBars)applicationObject.CommandBars;
            standardCommandBar = commandBars[VS_STANDARD_COMMANDBAR_NAME];

            // Add a button to the commandbar
            myStandardCommandBarButton = (CommandBarButton)myCommand.AddControl(standardCommandBar,
               standardCommandBar.Controls.Count + 1);

            // Set the properties of the button
            myStandardCommandBarButton.Caption = MY_COMMAND_CAPTION;
            myStandardCommandBarButton.BeginGroup = true;

            // There is no need to set this property
            // myStandardCommandBarButton.Style = MsoButtonStyle.msoButtonIcon;

         }
         catch (System.Exception ex)
         {
            System.Windows.Forms.MessageBox.Show(ex.ToString());
         }
      }

      public void OnDisconnection(Extensibility.ext_DisconnectMode RemoveMode, ref System.Array custom)
      {
         try
         {
            switch (RemoveMode)
            {
               case ext_DisconnectMode.ext_dm_HostShutdown:
               case ext_DisconnectMode.ext_dm_UserClosed:

                  if ((myStandardCommandBarButton != null))
                  {
                     myStandardCommandBarButton.Delete(true);
                  }
           
                  break;
            }
         }
         catch (System.Exception ex)
         {
            System.Windows.Forms.MessageBox.Show(ex.ToString());
         }
      }

      public void OnBeginShutdown(ref System.Array custom)
      {
      }

      public void OnAddInsUpdate(ref System.Array custom)
      {
      }

      public void Exec(string cmdName, vsCommandExecOption executeOption, ref object varIn,
         ref object varOut, ref bool handled)
      {

         handled = false;

         if ((executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault))
         {
            if (cmdName == addInInstance.ProgID + "." + MY_COMMAND_NAME)
            {
               handled = true;
               System.Windows.Forms.MessageBox.Show("Command executed.");
            }
         }
      }

      public void QueryStatus(string cmdName, vsCommandStatusTextWanted neededText,
         ref vsCommandStatus statusOption, ref object commandText)
      {
         if (neededText == vsCommandStatusTextWanted.vsCommandStatusTextWantedNone)
         {
            if (cmdName == addInInstance.ProgID + "." + MY_COMMAND_NAME)
            {
               statusOption = (vsCommandStatus)(vsCommandStatus.vsCommandStatusEnabled |
                  vsCommandStatus.vsCommandStatusSupported);
            }
            else
            {
               statusOption = vsCommandStatus.vsCommandStatusUnsupported;
            }
         }
      }

   }
}

Related articles



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


Top