0

My current set up has a class with only 2 variables currently (string type and int amount).

I override the ToString to print both of those things together. In my form I instantiate different instances of this class and populate the combo box just fine, it prints my override.

My question is how do I determine which instance is being selected? I can use selecteditem to retrieve my tostring override fine, but what if i want to alter the amount variable of a specific instance if its selected?

SelectedItem.Instance.VariableName

I imagine it would be something like this, I'm just not familiar with that syntax.

cmp
  • 420
  • 5
  • 22
Jaekx
  • 46
  • 8
  • 1
    See my answer to another question [here](http://stackoverflow.com/questions/41175071/entity-framework-6-c-sharp-passing-a-combobox-selectedvalue-as-a-parameter-for/41175291?noredirect=1#comment69609321_41175291) it will help you. – CodingYoshi Dec 18 '16 at 19:24
  • Is this WinForms?...something else? – Idle_Mind Dec 18 '16 at 19:31
  • Thank you @CodingYoshi that did help me I get how to use it now! – Jaekx Dec 18 '16 at 19:39

3 Answers3

0

You can add the object to the combobox and then use Display member to determine which property is shown.

https://msdn.microsoft.com/en-us/library/system.windows.forms.listcontrol.displaymember(v=vs.110).aspx

S. Walker
  • 2,129
  • 12
  • 30
  • Will this allow me to access a specific instance of a class and alter its values? Like i have a savings and a checking type of this class with differing amount values, depending on which instance is selected I can alter the amount integers respectively? This looks like its just returning which object is selected only. – Jaekx Dec 18 '16 at 19:28
0

Well, that depends on how you populate the data into your ComboBox. For instance, if you use data binding, you can do something like the following:

    Dictionary<string, YourClass> dict = new Dictionary<string, YourClass>();
    for (int x = 0; x <= 5; x++)
    {
        YourClass instance = new YourClass("Test", x);
        dict.Add(instance.ToString(), instance);
    }

    ComboBox1.DataSource = new BindingSource(dict, null);
    ComboBox1.DisplayMember = "key";
    ComboBox1.ValueMember = "value";

So you can easily interact with the interact with each instance based on the selected item of your ComboBox:

    Console.WriteLine(((YourClass)ComboBox1.SelectedValue).amount.ToString());

Hope that helps :)

0

Create a List<> of your instances and set that as the DataSource() of your ComboBox. Then you can retrieve the selected item, update it somehow, then reset the DataSource causing the ComboBox to display the new values:

    private List<Thing> things = new List<Thing>();

    private void Form1_Load(object sender, EventArgs e)
    {
        Thing thing1 = new Thing();
        thing1.Item = "Bob";
        thing1.Value = 411;

        Thing thing2 = new Thing();
        thing2.Item = "Joe";
        thing2.Value = -1;

        things.Add(thing1);
        things.Add(thing2);

        comboBox1.DataSource = things;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (comboBox1.SelectedIndex != -1)
        {
            Thing thing = (Thing)comboBox1.SelectedItem;
            // now do something with "thing":
            thing.Value = thing.Value + 1;

            // reset the ComboBox to update the entries:
            comboBox1.DataSource = null;
            comboBox1.DataSource = things;
            comboBox1.SelectedItem = thing;
        }
    }

With Class Thing:

public class Thing
{
    public string Item = "";
    public int Value = 0;

    public override string ToString()
    {
        return Item + ": " + Value.ToString();
    }
}
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40