如何将枚举值分配给Compact Framework中的.NET 1.1中的列表框? [英] How can I assign enum values to a listbox in .NET 1.1 on the Compact Framework?
问题描述
从 http://weblogs.asp.net/stevewellens/archive/2009/08/19/how-to-fill-a-listbox-dropdownlist-from-an-enum.aspx ,I请参阅将枚举值分配给列表框的示例:
From http://weblogs.asp.net/stevewellens/archive/2009/08/19/how-to-fill-a-listbox-dropdownlist-from-an-enum.aspx, I see this example for assigning enum values to a listbox:
Array Values = System.Enum.GetValues(EnumType);
foreach (int Value in Values)
{
string Display = Enum.GetName(EnumType, Value);
ListItem Item = new ListItem(Display, Value.ToString());
TheListBox.Items.Add(Item);
}
但是,在我的Windows CE项目(.NET 1.1)中,没有System.Enum.GetValues();有GetUnderlyingType和ToObject...
However, in my Windows CE project (.NET 1.1), there is no "System.Enum.GetValues()"; there is "GetUnderlyingType" and "ToObject"...
这是实现这一点的关键之一吗?
Is one of those the key to accomplishing this?
尝试实现Jon Skeet的代码时,ListItem声明将失败,无法找到类型或命名空间名称ListItem(您是否缺少一个使用指令或程序集引用?)
Trying to implement Jon Skeet's code, the "ListItem" declaration fails with "The type or namespace name 'ListItem' could not be found (are you missing a using directive or an assembly reference?)"
我正在使用OpenNETCF。所以,VS提供添加OpenNETCF.Windows.Forms.ListItem到我的使用块。但是当我这样做时,它会抱怨同一行的构造器部分(ListItem item = new ListItem(field.Name,value.ToString());)与:最好的重载方法匹配'OpenNETCF.Windows.Forms .ListItem.ListItem(string,int)'有一些无效的参数
I am using OpenNETCF. So, VS offers to add "OpenNETCF.Windows.Forms.ListItem" to my using block. But when I do, it then complains on the constructor part of that same line (ListItem item = new ListItem(field.Name, value.ToString());) with: "The best overloaded method match for 'OpenNETCF.Windows.Forms.ListItem.ListItem(string, int)' has some invalid arguments"
现在整个处理程序是:
private void PrinterPickerForm_Load(object sender, System.EventArgs e)
{
// Got this from Jon Skeet (http://stackoverflow.com/questions/17952900/how-can-i-assign-enum-values-to-a-listbox-in-net-1-1)
Type type = typeof(PrintUtils.BeltPrinterType);
foreach (FieldInfo field in type.GetFields(BindingFlags.Static | BindingFlags.Public))
{
// Fortunately unboxing to the enum's underlying field type works
int value = (int) field.GetValue(null);
ListItem item = new ListItem(field.Name, value.ToString());
listBoxBeltPrinters.Items.Add(item);
}
}
更新2
这样做:
UPDATE 2
This works:
private void PrinterPickerForm_Load(object sender, System.EventArgs e)
{
Type type = typeof(PrintUtils.BeltPrinterType);
foreach (FieldInfo field in type.GetFields(BindingFlags.Static | BindingFlags.Public))
{
string display = field.GetValue(null).ToString();
listBoxBeltPrinters.Items.Add(display);
}
}
确实,枚举本身没有填充列表框,只是代表他们的字符串,但没关系,因为我必须使用kludgy方式分配值:
It's true that the enums themselves are not populating the listboxes, just the strings that represent them, but that's okay, because I have to use a kludgy way to assign the value, too:
private void listBoxBeltPrinters_SelectedIndexChanged(object sender, System.EventArgs e)
{
//Tried to do something more "el[egant,oquent]" but .NET 1.1 seems to be holding me back
// http://stackoverflow.com/questions/17953173/how-can-i-assign-the-value-selected-in-a-listbox-to-an-enum-var/17953297?noredirect=1#17953297
string sel = listBoxBeltPrinters.SelectedItem.ToString();
if (sel == "Zebra QL220")
{
PrintUtils.printerChoice = PrintUtils.BeltPrinterType.ZebraQL220;
}
else if (sel == "ONiel")
{
PrintUtils.printerChoice = PrintUtils.BeltPrinterType.ONiel;
}
//else if ( . . .)
}
推荐答案
自从使用Compact Framework以来,已经很久了,但我怀疑最简单的方法只是获取字段值,并将它们存储在某个地方: p>
It's been a long time since I've used the Compact Framework, but I suspect the simplest approach is just to get the field values - and store them somewhere, of course:
Type type = typeof(EnumType);
foreach (FieldInfo field in type.GetFields(BindingFlags.Static |
BindingFlags.Public))
{
// Fortunately unboxing to the enum's underlying field type works
int value = (int) field.GetValue(null);
ListItem item = new ListItem(field.Name, value.ToString());
TheListBox.Items.Add(item);
}
这篇关于如何将枚举值分配给Compact Framework中的.NET 1.1中的列表框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!