When you set or retrieve a combo box’s value with the standard NSControl methods (such as setStringValue:, stringValue, setFloatValue:, and floatValue), you’re setting or retrieving the value of the combo box’s text field, and not the current selection of the list. Programmatically changing the combo box’s value does not change what’s selected in the combo box’s list. Conversely, programmatically changing what’s selected in the list does not change the text field’s value. If you want the text field value and the list selection to match up, you need to set them individually.
For example, say you want to initialize the combo box’s list and text field to the list’s third item. This Objective-C excerpt does that for a combo box that maintains an internal item list:
[myComboBox selectItemAtIndex:2]; // First item is at index 0 |
[myComboBox setObjectValue:[myComboBox objectValueOfSelectedItem]]; |
This Java excerpt does the same thing:
myComboBox.selectItemAtIndex(2);// First item is at index 0 |
myComboBox.setObjectValue(myComboBox.objectValueOfSelectedItem()); |
This Objective-C excerpt does that for a combo box with an external data source:
[myComboBox selectItemAtIndex:2]; |
[myComboBox setObjectValue: |
[myComboBoxDataSource comboBox:myComboBox |
ObjectValueForItemAtIndex:[myComboBox indexofSelectedItem]]]; |
This Java excerpt does the same thing:
myComboBox.selectItemAtIndex(2); |
myComboBox.setObjectValue( |
myComboBoxDataSource.comboBoxObjectValueForItemAtIndex( |
myComboBox, myComboBox.indexofSelectedItem())); |
And this Objective-C except initializes the combo box’s text field to “Red” and selects it from the list if available. Note that the excerpt works for combo boxes with either internal or external data sources:
[myComboBox setStringValue:@"Red"]; |
[myComboBox selectItemWithObjectValue:@"Red"]; |
This Java excerpt does the same thing:
myComboBox.setStringValue("Red"); |
myComboBox.selectItemWithObjectValue("Red"); |
Last updated: 2002-11-12