Wiki Navigation
- Loading...
Question
How do I show an Enum Selection Dialog?
Answer
To show a selection Dialog for values from a given Enum and returns the selected value. (using generics) Simply use the following code:
public T ShowEnumSelectionDialog<t>() { GUIDialogSelect2 dlgSelect = (GUIDialogSelect2)GUIWindowManager.GetWindow((int)GUIWindow.Window.WINDOW_DIALOG_SELECT2); dlgSelect.Reset(); dlgSelect.SetHeading("Selection: " + typeof(T).ToString()); Enum.GetNames(typeof(T)).ToList().ForEach(dlgSelect.Add); dlgSelect.DoModal(GUIWindowManager.ActiveWindow); try { return Enum<t>.Parse(dlgSelect.SelectedLabelText); } catch { return default (T); } } </t></t>
* 'T' must be of Enum type
Helper class for parsing enum value:
public static class Enum<t> { public static T Parse(string value) { return (T)Enum.Parse(typeof(T), value); } public static IList<t> GetValues() { IList<t> list = new List<t>(); foreach (object value in Enum.GetValues(typeof(T))) { list.Add((T)value); } return list; } } </t></t></t></t>
Usage:
MyEnum myEnumValue = ShowEnumSelectionDialog<myenum>();
This page has no comments.