【发布时间】:2013-07-30 17:07:43
【问题描述】:
我想避免以下的笨拙:
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 ( . . .)
}
有没有一种方法可以让我根据列表框选择更优雅或更雄辩地分配给枚举,例如:
PrintUtils.printerChoice = listBoxBeltPrinters.SelectedItem.ToEnum(PrintUtils.BeltPrinterType)?
?
【问题讨论】:
-
为什么不能直接将
enum object添加到ListBox -
@SriramSakthivel 因为 Enum.ToString 无法正确呈现“Zebra QL220”
-
您必须以任何一种方式妥协,如果有帮助,请查看我的更新解决方案,否则请随时询问