PHP-GTK2 Newsletter
PHP-GTK2 Tips & Techniques
FREE Newsletter
by kksou
Sample Code 308: How to setup GtkComboBox with label value pair - Part 1? |
|
Written by kksou
|
|
Sunday, 26 August 2007 |
|
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 35 36 37 38 39 40 41 42
| <?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
|
- Note that this is only 70% of the sample code. You have to be a registered member to see the entire sample code. Please login or register.
- Registration is free and immediate.
- Have some doubt about the registration? Please read this forum article.
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 06, 2008 12:47am