Like list widgets, combo-box widgets (CwComboBox) enable the user to select from a list of available items. A combo box also displays the last selected item in a text box above the list. Combo-box widgets can only have one item selected at a time. Combo-box widgets are created using the createComboBox:argBlock: convenience method.
There are two styles of combo boxes, specified by the comboBoxType resource:
As with the list widget, the items in the combo box are specified by the items resource. The application can add a singleSelection callback to be run whenever the selection changes. Several methods are provided for adding, deleting, and replacing items in the list.
The contents of the text part of the combo box can be set and retrieved using the setString: and getString methods.
The following example creates the drop down combo box shown at
right. Its items are set, the contents of the text box are initialized
to the first item, and a singleSelection callback is added.
| items shell combo | items := #('Item 1' 'Item 2' 'Item 3' 'Item 4'). shell := CwTopLevelShell createApplicationShell: 'shell' argBlock: [:w | w title: 'Combo Box Example']. |
![]() |
combo := shell createComboBox: 'combo' argBlock: [:w | w comboBoxType: XmDROPDOWN; items: items].
combo setString: items first. combo addCallback: XmNsingleSelectionCallback receiver: self selector: #singleSelect:clientData:callData: clientData: nil. combo manageChild. shell realizeWidget.
The call data of the singleSelection callback specifies the item that was selected. The callback method below prints the entire callback data on the transcript. All components of the call data can be retrieved using the corresponding accessor method.
singleSelect: widget clientData: clientData callData: callData "Print the call data." Transcript cr; show: 'Single selection call data: ', callData printString
If Item 2 is selected, as in the illustration, the transcript output is as follows:
Single selection call data: CwListCallbackData( reason -> 23 item -> 'Item 2' itemPosition -> 2 "These three fields of the callback data are only used for multiple and extended select lists." selectedItems -> nil selectedItemCount -> nil selectedItemPositions -> nil)