Apple Developer Connection
Member Login Log In | Not a Member? Contact ADC

< Previous PageNext Page > Hide TOC

Setting the Combo Box’s Value

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");


< Previous PageNext Page > Hide TOC


Last updated: 2002-11-12




Did this document help you?
Yes: Tell us what works for you.

It’s good, but: Report typos, inaccuracies, and so forth.

It wasn’t helpful: Tell us what would have helped.
Get information on Apple products.
Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Copyright © 2007 Apple Inc.
All rights reserved. | Terms of use | Privacy Notice