Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
190 views
in Technique[技术] by (71.8m points)

c# - How can I assign enum values to a listbox in .NET 1.1 on the Compact Framework?

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);
}

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?

UPDATE

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

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"

The entire handler now is:

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);
    }
}

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);
    }
}

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 ( . . .)
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can create your own GetValues method. From this blog post

private static int[] GetValues(Type enumType)
{
    if (enumType.BaseType == typeof (Enum))
    {
        //get the public static fields (members of the enum)
        var fi = enumType.GetFields(BindingFlags.Static | BindingFlags.Public);
        //create a new enum array
        var values = new int[fi.Length];
        //populate with the values
        for (var iEnum = 0; iEnum < fi.Length; iEnum++)
        {
            values[iEnum] = (int) fi[iEnum].GetValue(null);
        }
        //return the array
        return values;
    }

    //the type supplied does not derive from enum
    throw new ArgumentException("enumType parameter is not a System.Enum");
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...