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>

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

      How can I assign the value selected in a listbox to an enum var?(如何将列表框中选择的值分配给枚举变量?)
        <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;
                }
                

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

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Populate ListBox with a IEnumrable on another thread (winforms)(在另一个线程(winforms)上使用 IEnumrable 填充 ListBox)
                listbox selected item give me quot; System.Data.DataRowViewquot; , C# winforms(列表框选择的项目给我quot;System.Data.DataRowView, C# Winforms)
                Cannot remove items from ListBox(无法从列表框中删除项目)
                Preventing ListBox scrolling to top when updated(更新时防止列表框滚动到顶部)
                Drag and drop from list to canvas on windows phone with MVVM(使用 MVVM 在 Windows 手机上从列表拖放到画布)
                Deselection on a WPF listbox with extended selection mode(具有扩展选择模式的 WPF 列表框上的取消选择)

                • <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>
                        <tbody id='rpLur'></tbody>