1. <legend id='xyerC'><style id='xyerC'><dir id='xyerC'><q id='xyerC'></q></dir></style></legend>

    <small id='xyerC'></small><noframes id='xyerC'>

    • <bdo id='xyerC'></bdo><ul id='xyerC'></ul>
    <tfoot id='xyerC'></tfoot>
    <i id='xyerC'><tr id='xyerC'><dt id='xyerC'><q id='xyerC'><span id='xyerC'><b id='xyerC'><form id='xyerC'><ins id='xyerC'></ins><ul id='xyerC'></ul><sub id='xyerC'></sub></form><legend id='xyerC'></legend><bdo id='xyerC'><pre id='xyerC'><center id='xyerC'></center></pre></bdo></b><th id='xyerC'></th></span></q></dt></tr></i><div id='xyerC'><tfoot id='xyerC'></tfoot><dl id='xyerC'><fieldset id='xyerC'></fieldset></dl></div>

      如何将列表框中选择的值分配给枚举变量?

      时间:2023-10-08
        <tbody id='RzTyi'></tbody>
      <i id='RzTyi'><tr id='RzTyi'><dt id='RzTyi'><q id='RzTyi'><span id='RzTyi'><b id='RzTyi'><form id='RzTyi'><ins id='RzTyi'></ins><ul id='RzTyi'></ul><sub id='RzTyi'></sub></form><legend id='RzTyi'></legend><bdo id='RzTyi'><pre id='RzTyi'><center id='RzTyi'></center></pre></bdo></b><th id='RzTyi'></th></span></q></dt></tr></i><div id='RzTyi'><tfoot id='RzTyi'></tfoot><dl id='RzTyi'><fieldset id='RzTyi'></fieldset></dl></div>

    1. <small id='RzTyi'></small><noframes id='RzTyi'>

        <tfoot id='RzTyi'></tfoot>

          • <legend id='RzTyi'><style id='RzTyi'><dir id='RzTyi'><q id='RzTyi'></q></dir></style></legend>

                <bdo id='RzTyi'></bdo><ul id='RzTyi'></ul>
                本文介绍了如何将列表框中选择的值分配给枚举变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我想避免以下的笨拙:

                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)?
                

                ?

                推荐答案

                你可以试试这样的

                Array values = Enum.GetValues(typeof(BeltPrinterType));//If this doesn't help in compact framework try below code
                Array values = GetBeltPrinterTypes();//this should work, rest all same
                foreach (var item in values)
                {
                    listbox.Items.Add(item);
                }
                
                private static BeltPrinterType[] GetBeltPrinterTypes()
                {
                    FieldInfo[] fi = typeof(BeltPrinterType).GetFields(BindingFlags.Static | BindingFlags.Public);
                    BeltPrinterType[] values = new BeltPrinterType[fi.Length];
                    for (int i = 0; i < fi.Length; i++)
                    {
                        values[i] = (BeltPrinterType)fi[i].GetValue(null);
                    }
                    return values;
                    }
                
                private void listBoxBeltPrinters_SelectedIndexChanged(object sender, System.EventArgs e)
                {
                    if(!(listBoxBeltPrinters.SelectedItem is BeltPrinterType))
                    {
                        return;
                    }
                    PrintUtils.printerChoice = (BeltPrinterType)listBoxBeltPrinters.SelectedItem;
                }
                

                这篇关于如何将列表框中选择的值分配给枚举变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:如何禁用列表框中的选定项目 下一篇:如何在 WPF ListBox 中禁用水平滚动?

                相关文章

                • <bdo id='rpLur'></bdo><ul id='rpLur'></ul>
                <legend id='rpLur'><style id='rpLur'><dir id='rpLur'><q id='rpLur'></q></dir></style></legend>
              • <small id='rpLur'></small><noframes id='rpLur'>

                <tfoot id='rpLur'></tfoot>

                  <i id='rpLur'><tr id='rpLur'><dt id='rpLur'><q id='rpLur'><span id='rpLur'><b id='rpLur'><form id='rpLur'><ins id='rpLur'></ins><ul id='rpLur'></ul><sub id='rpLur'></sub></form><legend id='rpLur'></legend><bdo id='rpLur'><pre id='rpLur'><center id='rpLur'></center></pre></bdo></b><th id='rpLur'></th></span></q></dt></tr></i><div id='rpLur'><tfoot id='rpLur'></tfoot><dl id='rpLur'><fieldset id='rpLur'></fieldset></dl></div>