Is it possible to load items from an Enum to a ComboBox in .NET 3.5?
Asked
Active
Viewed 653 times
2
-
WinForms? ASP.Net? WPF? Silverlight? – SLaks Apr 25 '10 at 22:16
-
WinForms, your solution worked. :D THanks – Apr 25 '10 at 22:19
2 Answers
10
Yes.
combobox.DataSource = Enum.GetValues(typeof(SomeEnum));

Adam Robinson
- 182,639
- 35
- 285
- 343

SLaks
- 868,454
- 176
- 1,908
- 1,964
-
-
-
To complete answer, to get the value you do myVariable = (SomeEnum) comboBox1.SelectedValue; – blak3r Feb 28 '12 at 22:55
0
Here's some code we used on a recent project. It handles localized Enum strings (by passing in a ResourceManager
object) and populates the .Items
array directly instead of using a DataSource -- this is useful for populating a ComboBox
, including setting its .SelectedItem
, before making it or its parent controls visible.
public static void PopulateComboBox<T>(ComboBox box, ResourceManager res) {
box.FormattingEnabled = true;
ListControlConvertEventHandler del = delegate(object sender, ListControlConvertEventArgs e) {
e.Value = res.GetString(e.Value.ToString());
};
box.Format -= del;
box.Format += del;
box.BeginUpdate();
box.Items.Clear();
foreach(T value in Enum.GetValues(typeof(T))) {
box.Items.Add(value);
}
box.EndUpdate();
}
Use it like:
PopulateComboBox<MyEnum>(myComboBox, MyEnumStrings.ResourceManager);

Daniel Pryden
- 59,486
- 16
- 97
- 135