|
Problem This is in response to the note from yacatl from Mexico.
He wants to have a month combo showing the text months January, February, March, etc. When the user selects say March, he wants '03' to be returned instead of 'March' as shown below:

Solution
- You can use the method as highlighted in How to store additional value along with each option of pulldown menu? to store additional value in the model. There is some additional explanation of this method here.
- In case you are not too familiar with using data model with combobox, this artilce presents a simpler method. The effect is the same.
- We set up the combobox using standard GtkComboBox::new_text() to create a new combobox, and then GtkComboBox::append_text() to add each option of the combobox.
- When we process user selection, all we need is to do one more step of simple array look-up to find the corresponding value of the month.
Sample Code 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 36 37 46 47 49 50 51 52 53 54 55 56 59 60 61 62 63 64 65 68 69 70 71 72 73 74 75
| <?php $window = new GtkWindow(); $window->connect_simple('destroy', array( 'Gtk', 'main_quit')); $window->set_size_request(400,150);
$window->add($vbox = new GtkVBox());
// display title
$title = new GtkLabel("Setup GtkComboBox with label-value pair"); $title->modify_font(new PangoFontDescription("Times New Roman Italic 10")); $title->modify_fg(Gtk::STATE_NORMAL, GdkColor::parse("#0000ff")); $title->set_size_request(-1, 60); $vbox->pack_start($title, 0, 0);
$vbox->pack_start($hbox=new GtkHBox(), 0, 0); $hbox->pack_start(new GtkLabel('Select: '), 0, 0);
// the month label-value pair
$month_options = array( // note 1
'Januaary' => '01', 'February' => '02', 'March' => '03', 'April' => '04', 'May' => '05', 'June' => '06', 'July' => '07', 'August' => '08', 'September' => '09', 'October' => '10', 'November' => '11', 'December' => '12', );
// Create a combobox
$combobox = GtkComboBox::new_text(); // note 2
// populates the options
foreach($month_options as $label=>$value) { $combobox->append_text($label); // note 3
}
// Set up a hbox to contain the combobox as well as the Submit button
$hbox->pack_start($combobox, 0, 0); $hbox->pack_start(new GtkLabel(' '), 0, 0); $hbox->pack_start($button = new GtkButton('Submit'), 0, 0); $button->set_size_request(60, 24);
// Set up the event handler to respond to button click
$button->connect('clicked', "on_button", $combobox);
// The callback function that is called when user clicks the submit button
function on_button($button, $combobox) { global $month_options; $selection = $combobox->get_active_text(); // note 4
$value = $month_options[$selection]; // note 5
echo "You have selected: $selection (value = $value)!\n"; }
$window->show_all(); Gtk::main(); ?>
|
Explanation
- The months option in label-value pair. The left side is the label to be displayed. The right side is the corresponding value for each month.
- Create a new combobox.
- Populates the options.
- Get the combobox option selected by user.
- Get the corresponding value from the array
$month_options.
Related Links
User reviews Average user ratings: 5.0 (from 1 user) Note: You have to be a registered member to leave a comment. Free registration here. |
July 05, 2008 11:47pm