forked from gerito1/vala-gtk-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
combobox.vala
62 lines (48 loc) · 1.56 KB
/
combobox.vala
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
* Similar to a ComboBoxText, the ComboBox allows selection of items from a
* dropdown list. It provides more features, and is capable of displaying
* options of different types other than text.
*/
using Gtk;
public class Example : Window
{
private Gtk.ListStore liststore;
private ComboBox combobox;
public Example()
{
this.title = "ComboBox";
this.destroy.connect(Gtk.main_quit);
liststore = new Gtk.ListStore(1, typeof (string));
Gtk.TreeIter iter;
liststore.append(out iter);
liststore.set(iter, 0, "Rafael Nadal", -1);
liststore.append(out iter);
liststore.set(iter, 0, "Roger Federer", -1);
liststore.append(out iter);
liststore.set(iter, 0, "Novak Djokovic", -1);
var cellrenderertext = new CellRendererText();
combobox = new ComboBox();
combobox.set_model(liststore);
combobox.pack_start(cellrenderertext, true);
combobox.add_attribute(cellrenderertext, "text", 0);
combobox.set_active(0);
combobox.changed.connect(on_combobox_changed);
this.add(combobox);
}
private void on_combobox_changed()
{
Gtk.TreeIter treeiter;
Value val;
combobox.get_active_iter(out treeiter);
liststore.get_value(treeiter, 0, out val);
stdout.printf("Selection is '%s'\n", (string) val);
}
public static int main(string[] args)
{
Gtk.init(ref args);
var window = new Example();
window.show_all();
Gtk.main();
return 0;
}
}