I fill out a combo box with custom enum vals:
private enum AlignOptions { Left, Center, Right } . . . comboBoxAlign1.DataSource = Enum.GetNames(typeof(AlignOptions));
When I try to assign the selected element to the var variable of this enum type, however:
AlignOptions alignOption; . . . alignOption = (AlignOptions)comboBoxAlign1.SelectedItem;
... it explodes: "System.InvalidCastException was unhandled. Message = The specified cast is invalid."
Is the type AlignOptions?
UPDATE
Dang, I thought I was smart. Ginosaji is right, and I had to change it to:
alignOptionStr = comboBoxAlign1.SelectedItem.ToString(); if (alignOptionStr.Equals(AlignOptions.Center.ToString())) { lblBarcode.TextAlign = ContentAlignment.MiddleCenter; } else if (alignOptionStr.Equals(AlignOptions.Left.ToString())) { . . .
source share