Mercurial > repos > IBBoard.GtkSharp
diff ComboBoxUtils.cs @ 11:43d0b0ec1657
Re #26: GTK# wrappers
* Set and use indexes for string and value
* Add methods for returning selected value
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sat, 16 Jan 2010 16:14:07 +0000 |
parents | 3277cb2ecb71 |
children | ffda5e5f1617 |
line wrap: on
line diff
--- a/ComboBoxUtils.cs Sat Jan 16 15:32:49 2010 +0000 +++ b/ComboBoxUtils.cs Sat Jan 16 16:14:07 2010 +0000 @@ -12,23 +12,32 @@ public class ComboBoxUtils { + private static int DEFAULT_VALUE_COLUMN = 1; + private static int DEFAULT_TEXT_COLUMN = 0; + public static void FillCombo<LIST_TYPE>(ComboBox combo, IList<LIST_TYPE> itemList) { FillCombo(combo, itemList, delegate(LIST_TYPE obj){return obj.ToString();}); } - public static void FillCombo<LIST_TYPE>(ComboBox combo, IList<LIST_TYPE> itemList, ObjectToStringRenderingMethod<LIST_TYPE> toStringMethod) + public static void FillCombo<LIST_TYPE>(ComboBox combo, IList<LIST_TYPE> itemList, ObjectToStringRenderingMethod<LIST_TYPE> objectToString) { combo.Clear(); CellRendererText cell = new CellRendererText(); combo.PackStart(cell, false); combo.AddAttribute(cell, "text", 0); - ListStore store = new ListStore(typeof(string), typeof(LIST_TYPE)); + Type[] types = new Type[2]; + types[DEFAULT_VALUE_COLUMN] = typeof(LIST_TYPE); + types[DEFAULT_TEXT_COLUMN] = typeof(string); + ListStore store = new ListStore(types); combo.Model = store; foreach (LIST_TYPE item in itemList) { - store.AppendValues(toStringMethod(item), item); + object[] values = new object[2]; + values[DEFAULT_VALUE_COLUMN] = item; + values[DEFAULT_TEXT_COLUMN] = objectToString(item); + store.AppendValues(values); } } @@ -58,5 +67,17 @@ combo.Model.IterNthChild(out iter, idx); combo.SetActiveIter(iter); } + + public static LIST_TYPE GetSelectedItem<LIST_TYPE>(ComboBox combo) + { + return GetSelectedItem<LIST_TYPE>(combo, DEFAULT_VALUE_COLUMN); + } + + public static LIST_TYPE GetSelectedItem<LIST_TYPE>(ComboBox combo, int valueColumn) + { + TreeIter iter; + combo.GetActiveIter(out iter); + return (LIST_TYPE)combo.Model.GetValue(iter, valueColumn); + } } }