Compartir a través de


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)

Syntax

'Declaration
Function AddCommandBar ( _
    Name As String, _
    Type As vsCommandBarType, _
    CommandBarParent As Object, _
    Position As Integer _
) As Object
Object AddCommandBar(
    string Name,
    vsCommandBarType Type,
    Object CommandBarParent,
    int Position
)
Object^ AddCommandBar(
    [InAttribute] String^ Name, 
    [InAttribute] vsCommandBarType Type, 
    [InAttribute] Object^ CommandBarParent, 
    [InAttribute] int Position
)
abstract AddCommandBar : 
        Name:string * 
        Type:vsCommandBarType * 
        CommandBarParent:Object * 
        Position:int -> Object
function AddCommandBar(
    Name : String, 
    Type : vsCommandBarType, 
    CommandBarParent : Object, 
    Position : int
) : Object

Parameters

  • Name
    Type: System.String

    Required. A name for the new command bar.

  • Position
    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.

Remarks

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.

Examples

' 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", _
   vsCommandBarType.vsCommandBarTypeToolbar)
   MsgBox("Commandbar name: " & cmdbarobj.Name)
   cmdobj = cmds.AddNamedCommand(colAddins.Item(1), "mycommand", _
   "mycommand", "tooltip for mycommand", True)
End Sub

.NET Framework Security

See Also

Reference

Commands Interface

EnvDTE Namespace