Tuesday, January 24, 2006

Populating a combo box with enumerations

A simple function to populate a combo-box with the enumerations of a particular enum

private void populatecbo(System.Type enum_type, System.Windows.Forms.ComboBox combo)
{
Array arr_populate = Enum.GetValues(enum_type);
combo.DataSource = arr_populate;
}


A cast to type using typeof(enum_type) would be required during the call

Also retrieving the value would be easy

(enum_type)System.Enum.Parse(typeof(enum_type), combo.SelectedValue);

No comments: