This documentation is archived and is not being maintained.

Adding Buttons to a Toolbar

Visual Studio .NET 2003

An integral part of the ToolBar control is the buttons you add to it. These can be used to provide easy access to menu commands or, alternately, they can be placed in another area of the user interface of your application to expose commands to your users that are not available in the menu structure.

The examples below assume that a ToolBar control has been added to a Windows Form (Form1). For details, see Adding Controls to Windows Forms.

To add buttons at design time

  1. In the drop-down list at the top of the Properties window, select the ToolBar control you added to your form.
  2. Click the Buttons property to select it and click the Ellipsis (Aa984253.vbellipsesbutton(en-us,VS.71).gif) button to open the ToolBarButton Collection Editor.
  3. Use the Add and Remove buttons to add and remove buttons from the ToolBar control.
  4. Configure the properties of the individual buttons in the Properties window that appears in the pane on the right side of the editor. Important properties to consider include:
    PropertyDescription
    DropDown MenuSets the menu to be displayed in the drop-down toolbar button. The toolbar button's Style property must be set to DropDown Button. This property takes an instance of the ContextMenu class as a reference. See the code sample below for an example.
    PartialPushSets whether a toggle-style toolbar button is partially pushed. The toolbar button's Style property must be set to ToggleButton.
    PushedSets whether a toggle-style toolbar button is currently in the pushed state. The toolbar button's Style property must be set to ToggleButton or PushButton.
    StyleSets the style of the toolbar button. Must be one of the values in the ToolBarButtonStyle enumeration.
    TextThe text string displayed by the button.
    ToolTipTextThe text that appears as a ToolTip for the button.
  5. Click OK to close the dialog box and create the panels you specified.

To add buttons programmatically

  1. In a procedure, create toolbar buttons by adding them to the ToolBarButtons collection.
  2. Specify property settings for an individual button by passing the button's index via the Buttons property.

    The example below assumes a form with a ToolBar control already added.

    Note   The ToolBarButtons collection is a zero-based collection, so code should proceed accordingly.
    ' Visual Basic
    Public Sub CreateToolBarButtons()
    ' Create buttons and set text property.
       ToolBar1.Buttons.Add("One")
       ToolBar1.Buttons.Add("Two")
       ToolBar1.Buttons.Add("Three")
       ToolBar1.Buttons.Add("Four")
    ' Set properties of StatusBar panels.
    ' Set Style property.
       ToolBar1.Buttons(0).Style = ToolBarButtonStyle.PushButton
       ToolBar1.Buttons(1).Style = ToolBarButtonStyle.Separator
       ToolBar1.Buttons(2).Style = ToolBarButtonStyle.ToggleButton
       ToolBar1.Buttons(3).Style = ToolBarButtonStyle.DropDownButton
    ' Set the ToggleButton's PartialPush property.
       ToolBar1.Buttons(2).PartialPush = True
    ' Instantiate a ContextMenu component and menu items.
    ' Set the DropDownButton's DropDownMenu property to the context menu.
       Dim cm As New ContextMenu()
       Dim miOne As New MenuItem("One")
       Dim miTwo As New MenuItem("Two")
       Dim miThree As New MenuItem("Three")
       cm.MenuItems.Add(miOne)
       cm.MenuItems.Add(miTwo)
       cm.MenuItems.Add(miThree)
       ToolBar1.Buttons(3).DropDownMenu = cm
    ' Set the PushButton's Pushed property.
       ToolBar1.Buttons(0).Pushed = True
    ' Set the ToolTipText property of one of the buttons.
       ToolBar1.Buttons(1).ToolTipText = "Button 2"
    End Sub
    
    // C#
    public void CreateToolBarButtons()
    {
       // Create buttons and set text property.
       toolBar1.Buttons.Add("One");
       toolBar1.Buttons.Add("Two");
       toolBar1.Buttons.Add("Three");
       toolBar1.Buttons.Add("Four");
    
       // Set properties of StatusBar panels.
       // Set Style property.
       toolBar1.Buttons[0].Style = ToolBarButtonStyle.PushButton;
       toolBar1.Buttons[1].Style = ToolBarButtonStyle.Separator;
       toolBar1.Buttons[2].Style = ToolBarButtonStyle.ToggleButton;
       toolBar1.Buttons[3].Style = ToolBarButtonStyle.DropDownButton;
    
       // Set the ToggleButton's PartialPush property.
       toolBar1.Buttons[2].PartialPush = true;
    
       // Instantiate a ContextMenu component and menu items.
       // Set the DropDownButton's DropDownMenu property to 
       // the context menu.
       ContextMenu cm = new ContextMenu();
       MenuItem miOne = new MenuItem("One");
       MenuItem miTwo = new MenuItem("Two");
       MenuItem miThree = new MenuItem("Three");
       cm.MenuItems.Add(miOne);
       cm.MenuItems.Add(miTwo);
       cm.MenuItems.Add(miThree);
    
       toolBar1.Buttons[3].DropDownMenu = cm;
       // Set the PushButton's Pushed property.
       toolBar1.Buttons[0].Pushed = true;
       // Set the ToolTipText property of 1 of the buttons.
       toolBar1.Buttons[1].ToolTipText = "Button 2";
    }
    
    // C++
    public:
       void CreateToolBarButtons()
       {
          // Create buttons and set text property.
          toolBar1->Buttons->Add(S"One");
          toolBar1->Buttons->Add(S"Two");
          toolBar1->Buttons->Add(S"Three");
          toolBar1->Buttons->Add(S"Four");
    
          // Set properties of StatusBar panels.
          // Set Style property.
          toolBar1->Buttons->Item[0]->Style =
             ToolBarButtonStyle::PushButton;
          toolBar1->Buttons->Item[1]->Style =
             ToolBarButtonStyle::Separator;
          toolBar1->Buttons->Item[2]->Style =
             ToolBarButtonStyle::ToggleButton;
          toolBar1->Buttons->Item[3]->Style =
             ToolBarButtonStyle::DropDownButton;
    
          // Set the ToggleButton's PartialPush property.
          toolBar1->Buttons->Item[2]->PartialPush = true;
    
          // Instantiate a ContextMenu component and menu items.
          // Set the DropDownButton's DropDownMenu property to 
          // the context menu.
          System::Windows::Forms::ContextMenu * cm = new
             System::Windows::Forms::ContextMenu();
          MenuItem * miOne = new MenuItem(S"One");
          MenuItem * miTwo = new MenuItem(S"Two");
          MenuItem * miThree = new MenuItem(S"Three");
          cm->MenuItems->Add(miOne);
          cm->MenuItems->Add(miTwo);
          cm->MenuItems->Add(miThree);
    
          toolBar1->Buttons->Item[3]->DropDownMenu = cm;
          // Set the PushButton's Pushed property.
          toolBar1->Buttons->Item[0]->Pushed = true;
          // Set the ToolTipText property of 1 of the buttons.
          toolBar1->Buttons->Item[1]->ToolTipText = S"Button 2";
       }
    

See Also

Defining an Icon for a Toolbar Button | Triggering Menu Events for Toolbar Buttons | ToolBar Class | Introduction to the Windows Forms ToolBar Control | ToolBar Control (Windows Forms)

Show: