Automatically Fill a ComboBox with an Enum

This is a really nice little feature.

If we have an enumerator, say:

public enum MyEnumerator { Oranges, Apples, Hedgehogs, Laptops, Budgies, Other }

Having a form with a ComboBox called comboBox1, put this in the constructor/form load etc:
this.comboBox1.DataSource = Enum.GetValues(typeof(MyEnumerator));

This fills the combobox with the values. To convert the current combobox value back into the enumerator’s value:
MyEnumerator selected = (MyEnumerator)this.comboBox1.SelectedValue;

Creative Commons License
This work, unless otherwise expressly stated, is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 License.