Visual Basic: Windows Controls

Toolbar Control Example

This example adds Button objects to a Toolbar control using the Add method and assigns images supplied by the ImageList control. The behavior of each button is determined by the Style property. The code creates buttons that can be used to open and save files and includes a ComboBox control that is used to change the backcolor of the form. To try the example, place a Toolbar, ImageList, and a ComboBox on a form and paste the code into the form's Declarations section. Make sure that you insert the ComboBox directly on the Toolbar control. Run the example, click the various buttons and select from the combo box.

Private Sub Form_Load()
   ' Create object variable for the ImageList.
   Dim imgX As ListImage

   ' Load pictures into the ImageList control.
   Set imgX = ImageList1.ListImages. _
   Add(, "open", LoadPicture("Graphics\bitmaps\tlbr_w95\open.bmp"))
   Set imgX = ImageList1.ListImages. _
   Add(, "save", LoadPicture("Graphics\bitmaps\tlbr_w95\save.bmp"))
   Toolbar1.ImageList = ImageList1

   ' Create object variable for the Toolbar.
   Dim btnX As Button
   ' Add button objects to Buttons collection using 
    ' the
   ' Add method. After creating each button, set both
   ' Description and ToolTipText properties.
   Toolbar1.Buttons.Add , , , tbrSeparator
   Set btnX = Toolbar1.Buttons.Add(, "open", , tbrDefault, "open")
   btnX.ToolTipText = "Open File"
   btnX.Description = btnX.ToolTipText
   Set btnX = Toolbar1.Buttons.Add(, "save", , tbrDefault, "save")
   btnX.ToolTipText = "Save File"
   btnX.Description = btnX.ToolTipText
   Set btnX = Toolbar1.Buttons.Add(, , , tbrSeparator)
 
   ' The next button has the Placeholder style. A 
    ' ComboBox control will be placed on top of this 
    ' button.
   Set btnX = Toolbar1.Buttons.Add(, "combo1", , tbrPlaceholder)
   btnX.Width = 1500 ' Placeholder width to accommodate a combobox.

   Show ' Show form to continue configuring ComboBox.

   ' Configure ComboBox control to be at same location 
    ' as the
   ' Button object with the PlaceHolder style (key = 
     ' "combo1").
   With Combo1
      .Width = Toolbar1.Buttons("combo1").Width
      .Top = Toolbar1.Buttons("combo1").Top
      .Left = Toolbar1.Buttons("combo1").Left
      .AddItem "Black" ' Add colors for text.
      .AddItem "Blue"
      .AddItem "Red"
      .ListIndex = 0
   End With

End Sub

Private Sub Form_Resize()
   ' Configure ComboBox control.
   With Combo1
      .Width = Toolbar1.Buttons("combo1").Width
      .Top = Toolbar1.Buttons("combo1").Top
      .Left = Toolbar1.Buttons("combo1").Left
   End With

End Sub
Private Sub toolbar1_ButtonClick(ByVal Button As Button)
   ' Use the Key property with the SelectCase statement to specify
   ' an action.
   Select Case Button.Key
   Case Is = "open"       ' Open file.
      MsgBox "Add code to open file here!"
   Case Is = "save"        ' Save file.
      MsgBox "Add code to save file here!"
   End Select
End Sub

Private Sub Combo1_Click()
   ' Change backcolor of form using the ComboBox.
   Select Case Combo1.ListIndex
   Case 0
      Form1.BackColor = vbBlack
   Case 1
      Form1.BackColor = vbBlue
   Case 2
      Form1.BackColor = vbRed
   End Select
End Sub
Show: