Working with a CommandBarComboBox

The CommandBarComboBox is very similar to a standard ComboBox control, except that it exists only on a CommandBar control. You create and add a CommandBarComboBox the same way you add a CommandBarButton.

The following code example shows how to add a combo box to a CommandBar control.

Dim C as CommandBarComboBox
Set C = CommandBar1.Controls.Add(cbrComboBox)

Once the combo box has been added, you can change it to one of two styles, cbrComboDropDown or cbrComboDropDownList, by setting the Style property. The following code example shows how to change the style of a CommandBarComboBox.

C.Style = cbrComboBoxDropDownList

Once you place the combo box on the CommandBar, you can fill it with information and receive event notifications. The CommandBar shares events for all combo boxes on the CommandBar. The following code example shows the syntax for the CommandBarComboBox events.

Private Sub object_ComboBoxChange(ByVal ComboBox As CommandBarComboBox)
Private Sub object_ComboBoxClick(ByVal ComboBox As CommandBarComboBox)

Because the events pass the CommandBarComboBox object by value, you cannot make changes directly to the combo box. Instead, you can use the Key property to identify a control in the CommandBarControls collection.

The following code example shows how to set the Key property of a CommandBarComboBox and use it to identify which event occurred.

Dim C as CommandBarComboBox
Set C = CommandBar1.Controls.Add(cbrComboBox,"ComboOne")
. . .
Private Sub CommandBar1_ComboboxClick _
  (ByVal ComboBox As CommandBarCombobox)
  If ComboBox.Key = "ComboOne" Then MsgBox "It was combo box 'C' "
End Sub