简体   繁体   中英

Trying to bind a combobox to an enum in C#

I have a class that contains a property that is an enum:

public RaTypes RaBucket1Type { get; set; }

My enum is:

    public enum RaTypes
{
    Red,
    Yellow
}

I was able to bind a form's combobox data-source to the enum so that when I click on the drop-down, I see the enumerations:

cmbBucket1Type.DataSource = Enum.GetValues(typeof(RaTypes));

When I load the form, I would like to populate the combo-box with the existing value. I have tried the following:

        cmbBucket1Type.DisplayMember = "TradeType";
        cmbBucket1Type.ValueMember = "TradeEnumID";
        cmbBucket1Type.SelectedValue = EditedAlgorithm.RaBucket1Type;

But this did not work.

Also, I'm not sure I have implemented the ValueChanged event handler correctly either:

EditedAlgorithm.RaBucket1Type = (RaTypes)((ComboBox)sender).SelectedItem;

Can someone help me understand:

  1. How to set the combobox to current value, and
  2. How to handle the event handler so I can set the property to whatever was selected?

Thanks -Ed

UPDATES I have tried

    cmbBucket1Type.SelectedIndex = cmbBucket1Type.FindString(EditedAlgorithm.RaBucket1Type.ToString());

and

    cmbBucket1Type.SelectedItem = EditedAlgorithm.RaBucket1Type;

Neither works.

You can set the selectedValue like this:

cmbBucket1Type.SelectedValue = EditedAlgorithm.RaBucket1Type;

And you can handle the selected value when the combo change like this:

        private void cmbBucket1Type_SelectedValueChanged(object sender, EventArgs e)
    {
        var selectedValue = cmbBucket1Type.SelectedValue;
    }

I think you're using the terminology a little differently than normal, which makes it difficult to understand.

Normally, the terms Add , Populate , and Select are used to mean the following:

  • Add - Add an item to the existing set of items in the combo box.
  • Populate - Initialize the combo box with a set of items.
  • Select (Display) - Choose one among many items in the combo box as the selected item. Normally this item will be displayed in the combo box visible area.

Having cleared that up, I assume following is what you want to do.

  1. Initially populate the ComboBox with a set of values. In your case, values of RaType Enum .
  2. Create an instance of your class which contains the property mentioned. Since you didn't name that class I'll simply name it SomeClass .
  3. Initialize the RaBucket1Type property of the said class instance with an enum value of your choice. I'll initialize it to Yellow .
  4. Have the ComboBox select the said value at start up.
  5. After Form_Load , at any given time, if the user changes the value of the ComboBox , have the change reflected in your class instance property.

For that, I would do something like this:

public partial class MainForm : Form
{
    // Your class instance.
    private SomeClass InstanceOfSomeClass = null;

    public MainForm()
    {
        InitializeComponent();
        // Initialize the RaBucket1Type property with Yellow.
        InstanceOfSomeClass = new SomeClass(RaTypes.Yellow);
        // Populating the ComboBox
        comboBox1.DataSource = Enum.GetValues(typeof(RaTypes));
    }

    // At selected index changed event
    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        // Get the selected value.
        var selected = comboBox1.SelectedValue;
        // Change the `RaBucket1Type` value of the class instance according to the user choice.
        InstanceOfSomeClass.RaBucket1Type = (RaTypes)selected;
    }

    private void MainForm_Load(object sender, EventArgs e)
    {
        // At form load time, set the `SelectedItem` of the `ComboBox` to the value of `RaBucket1Type` of your class instance.
        // Since we initialized it to `Yellow`, the `ComboBox` will show `Yellow` as the selected item at load time.
        if (InstanceOfSomeClass != null)
        {
            comboBox1.SelectedItem = InstanceOfSomeClass.RaBucket1Type;
        }
    }
}

public enum RaTypes
{
    Red,
    Yellow
}

public class SomeClass
{
    public RaTypes RaBucket1Type { get; set; }

    public SomeClass(RaTypes raTypes) { RaBucket1Type = raTypes; }
}

Please do keep in mind this is a basic example to show you how to handle the situation and not a complete finished code. You'll need to do a bunch of error checks to make sure class instances and selected items are not null etc.

I FOUND MY ANSWER:

I had the SelectedIndexChanged event pointing to my event handler which means that when I "added" items to the ComboBox using:

comboBox1.DataSource = Enum.GetValues(typeof(RaTypes));

it was triggering the event handler, and resetting my class property. My event handler was this:

var selectedValue = cmbBucket1Type.SelectedValue;

So the simple solution was to:

  1. Remove the hard-coded event handler from the Visual Studio GUI.
  2. Add the following event handler in code AFTER I assign the DataSource

    bucketType1.SelectedIndexChanged += BucketTypeChanged;

This worked.

THANK YOU ALL FOR HELPING!!

-Ed

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM