I've got a helper class to convert an enum into a list.

class EnumHelper
    {
        public static List<T> EnumToList<T>()
        {
            Type enumType = typeof(T);

            // Can't use type constraints on value types, so have to do check like this
            if (enumType.BaseType != typeof(Enum))
            {
                throw new ArgumentException("T must be of type System.Enum");
            }

            return new List<T>(Enum.GetValues(enumType) as IEnumerable<T>);
        }
    }

I can create a list using the ActorType enum.

List<ActorType> listActorType = EnumHelper.EnumToList<ActorType>();
            
            for (int i = 0; i < listActorType.Count; ++i)
            {
                comboBox_Type.Items.Add(listActorType[i]);
            }

I would like to select an enum based on the index of the list, is this possible?

comboBox_Type.Items[cb.ItemIndex] ..... select correct enum

The GUI code I am working with has a generic list of objects for the Items it holds, so I can't see a way around this.

I would like to select an enum based on the index of the list

You would need some sort of mapping between index values and enum values for that.

Since you actually fill combobox with enum values, all you need is to cast combobox items back to enums:

class EnumHelper
{
    public static List<T> EnumToList<T>()
    {
        Type enumType = typeof(T);

        // Can't use type constraints on value types, so have to do check like this
        if (enumType.BaseType != typeof(Enum))
        {
            throw new ArgumentException("T must be of type System.Enum");
        }

        return new List<T>(Enum.GetValues(enumType) as IEnumerable<T>);
    }
}

// DEBUG:
public enum ActorType
{
    foo = 2,
    bar = 5
}

private void button1_Click(object sender, EventArgs e)
{
    List<ActorType> listActorType = EnumHelper.EnumToList<ActorType>();

    // DEBUG:
    comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;

    for (int i = 0; i < listActorType.Count; ++i)
    {
        comboBox1.Items.Add(listActorType[i]);
    }

    comboBox1.SelectedIndexChanged += new EventHandler(comboBox1_SelectedIndexChanged);
}

void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    ActorType enumOfActorType;

    enumOfActorType = (ActorType)comboBox1.SelectedItem;

    // DEBUG:
    MessageBox.Show("Enum name: " + enumOfActorType.ToString() + Environment.NewLine +
        "Enum value: " + ((int)enumOfActorType).ToString());
}

HTH

That worked great. I just used the item index (int) and cast from that. Thank you very much.

private void ComboBoxTypeItemIndexChanged(object sender, System.EventArgs e)
        {
            TomShane.Neoforce.Controls.ComboBox cb = sender as TomShane.Neoforce.Controls.ComboBox;

            if (objectSelected != null)
            {
                objectSelected.Type = (ActorType)cb.ItemIndex;
            }
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.