public static T GetEnumValue <T>(this ComboBox comboBox) where T : struct, IConvertible { var item = comboBox.SelectedItem; if (item == null || item.GetType() != typeof(TagString)) { return(default(T)); } TagString ts = item as TagString; if (ts.Tag == null || ts.Tag.GetType() != typeof(T)) { return(default(T)); } return((T)ts.Tag); }
public static void InitForEnum <T>(this ComboBox comboBox, T initialValue) where T : struct, IConvertible { comboBox.Items.Clear(); TagString selectItem = null; foreach (T val in Enum.GetValues(typeof(T))) { var item = new TagString(EnumUtil.GetEnumDesc <T>(val), val); if (val.Equals(initialValue)) { selectItem = item; } comboBox.Items.Add(item); } if (selectItem != null) { comboBox.SelectedItem = selectItem; } }