I want to avoid the kludginess of:
private void listBoxBeltPrinters_SelectedIndexChanged(object sender, System.EventArgs e)
{
string sel = string listBoxBeltPrinters.SelectedItem.ToString();
if (sel == "Zebra QL220")
{
PrintUtils.printerChoice = PrintUtils.BeltPrinterType.ZebraQL220;
}
else if (sel == "ONiel")
{
PrintUtils.printerChoice = PrintUtils.BeltPrinterType.ONiel;
}
else if ( . . .)
}
Is there a way I can more elegantly or eloquently assign to an enum based on a list box selection, something like:
PrintUtils.printerChoice = listBoxBeltPrinters.SelectedItem.ToEnum(PrintUtils.BeltPrinterType)?
?
With Enum.Parse you could convert from a string to a Enum.
Also there is method Enum.TryParse which returns a bool indicating if the parse is succeeded.
You could try something like this