Click here to Skip to main content
15,892,737 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
public enum Colour
    {
        red,
        blue,
        green
    }

public enum Taste
    {
        sour,
        salt,
        blue
    }

etc.


I'm having trouble figuring out how to bind these two enums to one list simultaneously. Would it be solved by using an array? Normally, .DataSource = Enum.GetValues(typeof(Colour)) would suffice of course. Is there any way to include a second in that syntax?
Posted
Comments
Sergey Alexandrovich Kryukov 6-Feb-13 17:24pm    
Maybe your idea of having two separate enum types is wrong?
—SA
leprechauny 6-Feb-13 17:30pm    
No. It's quite necessary actually. The thing I'm after is a function like "list all types" checkbox.

Please see my comment to the question.

For other ideas, I can suggest two sources. First, look at my past answers collected in one: combobox.selectedvalue shows {} in winform[^].

The idea is: you should create a data class which can use both enumeration types and some other data, to put instances of this type to the list. My answers referenced above show how to render use such type and render some appropriate text in the list.

The second source of ideas you can find in my related articles:
Enumeration Types do not Enumerate! Working around .NET and Language Limitations[^],
Human-readable Enumeration Meta-data[^].

Anyway, I think your best approach should start from the reviewing of your enumeration types, and data types in general. One marginal idea is merging them as shown in my first of the articles referenced above.

Good luck,
—SA
 
Share this answer
 
Comments
leprechauny 6-Feb-13 17:39pm    
Cheers, I'll go through your articles. Thanks!
Sergey Alexandrovich Kryukov 6-Feb-13 17:44pm    
Sure. And consider to eventually accept the answer formally (green button) — thanks.
—SA
Why not just use comboBox.Items.Add(myObject)? Like this...

C#
public Form1()
{
    InitializeComponent();
    ComboBox cb = new ComboBox();
    Colours[] colours = (Colours[])(Enum.GetValues(typeof(Colours)));
    Tastes[] tastes = (Tastes[])(Enum.GetValues(typeof(Tastes)));
    foreach (Colours o in colours)
    {
        cb.Items.Add(o);
    }
    foreach (Tastes o in tastes)
    {
        cb.Items.Add(o);
    }
    this.Controls.Add(cb);
    cb.SelectedIndexChanged += new EventHandler(cb_SelectedIndexChanged);
}


With this being said we now need to write a method to determine which Enum our value comes from. :)

C#
private Colours CurrentColor;
private Tastes CurrentTaste;
private Colours? GetColour(ComboBox cb)
{
    Colours colour;
    if (cb.SelectedItem != null)
    {
            if(!Enum.TryParse<Colours>(cb.SelectedItem.ToString(), out colour))
            {
                return null;
            }
            return colour;
    }
    return null;
}
private Tastes? GetTastes(ComboBox cb)
{
    Tastes taste;
    if (cb.SelectedItem != null)
    {
        if (!Enum.TryParse<Tastes>(cb.SelectedItem.ToString(), out taste))
        {
            return null;
        }
        return taste;
    }
    return null;
}

Now all we have to do is call these methods from comboBox.SelectedIndexChanged as such

C#
void cb_SelectedIndexChanged(object sender, EventArgs e)
        {
            ComboBox cb = (ComboBox)sender;
            Colours? colour = this.GetColour(cb);
            Tastes? taste = this.GetTastes(cb);
            if (taste.HasValue)
            {
                this.CurrentTaste = taste.Value;
            }
            if (colour.HasValue)
            {
                this.CurrentColor = colour.Value;
            }
        }
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900