1.Start a new Visual Basic project. Form1 is created by default.
2.Place a ComboBox on the form.
3.Place a Toolbar on the form.
4.Add the following code to the Form1 code window:
Option Explicit
Private Sub Form_Load()
Dim btn As Button
Me.Show
Set btn = Toolbar1.Buttons.Add()
btn.Style = tbrSeparator
Set btn = Toolbar1.Buttons.Add()
btn.Style = tbrPlaceholder
btn.Key = "ComboBox"
btn.Width = 2000
DoEvents
With Combo1
.ZOrder 0
.Width = Toolbar1.Buttons("ComboBox").Width
.Top = Toolbar1.Buttons("ComboBox").Top
.Left = Toolbar1.Buttons("ComboBox").Left
End With
End Sub
5.Press the F5 key to run the project. The Form should load with a ComboBox in the Toolbar.
Private Sub Form_Load()
Dim i As Integer
Dim btn As Button
' 添加五个 Button 对象到 Toolbar 控件。
For i = 1 To 5
Set btn = Toolbar1.Buttons.Add(Caption:= i, Style:= tbrDropDown)
' 添加两个 ButtonMenu 对象到每一个Button。
btn.ButtonMenus.Add Text:="Help"
btn.ButtonMenus.Add Text:="Options"
Next i
End Sub
Private Sub Toolbar1_ButtonMenuClick(ByVal ButtonMenu As ComctlLib.ButtonMenu)
Select Case ButtonMenu.Index
Case 1
MsgBox "Press the button."
Case 2
MsgBox "Offer some option"
End Select
End Sub