Commands.AddCommandBar Method

Creates a command bar that is saved and available the next time the environment is started.

Namespace:  EnvDTE
Assembly:  EnvDTE (in EnvDTE.dll)

Object AddCommandBar(
	string Name,
	vsCommandBarType Type,
	Object CommandBarParent,
	int Position


Type: System.String
Required. A name for the new command bar.
Type: EnvDTE.vsCommandBarType
Required. A vsCommandBarType constant that determines the type of command bar:
Type: System.Object
Optional. An Office CommandBar object to which the new command bar is to be added. (This is required, however, if Type is vsCommandBarTypeMenu.)
Type: System.Int32
Optional. The index position, starting at one, in the command bar to place the new command bar.

Return Value

Type: System.Object
A CommandBar object.

The CommandBar object is a member of the Microsoft Office object model.

The command bar added by using this method is a permanent command bar, which is available in every session of the IDE whether or not the add-in is loaded. You should use this method to add a command bar only if you want a permanent command bar. You should call this method only once, when your add-in is loaded for the first time. If you want a temporary command bar, which appears only when the add-in is actually loaded, you must use the DTE.CommandBars.Add method when you load the add-in, and then call DTE.CommandBars.Remove method when you unload the add-in.

Since a permanent command bar appears even when the add-in is not loaded, you should be sure to remove it when the add-in is uninstalled. Therefore, you must use an MSI to install and uninstall your add-in, and add a custom action to your uninstall program.

For more information about adding and removing permanent and temporary command bars, see HOWTO: Adding buttons, commandbars and toolbars to Visual Studio .NET from an add-in and HOWTO: Removing commands and UI elements during Visual Studio .NET add-in uninstallation.

' Macro code.
Imports Microsoft.VisualStudio.CommandBars
Sub AddCommandBarExample()
   ' Before running, you must add a reference to the Office 
   ' typelib to gain access to the CommandBar object. Also, for this 
   ' example to work correctly, there should be an add-in available 
   ' in the Visual Studio environment.
   Dim cmds As Commands
   Dim cmdobj As Command
   Dim customin, customout As Object
   Dim cmdbarobj As CommandBar
   Dim colAddins As AddIns

   ' Set references.
   colAddins = DTE.AddIns()
   cmds = DTE.Commands
   cmdobj = cmds.Item("File.NewFile")

   ' Create a toolbar and add the File.NewFile command to it.
   cmdbarobj = cmds.AddCommandBar("Mycmdbar", _
   MsgBox("Commandbar name: " & cmdbarobj.Name)
   cmdobj = cmds.AddNamedCommand(colAddins.Item(1), "mycommand", _
   "mycommand", "tooltip for mycommand", True)
End Sub

Community Additions