C# Windows Forms - How to get previous SelectedItem in Combobox

Viewed 139

I'm currently trying to figure out how to get the previous Selected Item in a Combobox, the data is added in a list in the Form1_Load function.

  //Flavour Change Button
    private void CoboFlav_SelectionChangeCommitted(object sender, EventArgs e)
    {
        var selectedItemPrice = (coboFlav.SelectedItem as Flavour).price;
        var selectedItemName = (coboFlav.SelectedItem as Flavour).name;

        var pre_item = pre_selIndex = (coboFlav.SelectedItem as Flavour).price;

        //var previousItem = flavourTea_Previous_Var = (coboFlav.SelectedItem as Flavour).price;

        //Item List
        Flavour listItem1 = ListCopy.MyList2.Find(x => (x.name == "- None -"));
        Flavour listItem2 = ListCopy.MyList2.Find(x => (x.name == "Lemon"));
        Flavour listItem3 = ListCopy.MyList2.Find(x => (x.name == "Passionfruit"));
        Flavour listItem4 = ListCopy.MyList2.Find(x => (x.name == "Yogurt"));

        //Checking Base Tea Box for adding price to currentItemTotal
        if (coboFlav.Text == listItem1.name || coboFlav.Text == listItem2.name || coboFlav.Text == listItem3.name || coboFlav.Text == listItem4.name)
        {
            //Increment Item Cost Value & take away previous item cost 
            currentTotalItemCost += selectedItemPrice - pre_item;
        }


        //Update CUrrentTotal Text
        CurrentTotal.Text = currentTotalItemCost.ToString();
    }

If the user selected an option in the combobox the selectedPrice int is increased. I am trying to take away the previousItemCost and im having trouble understanding how to find the previous selected user input.

I am not really sure how to approach this, I have seen a couple of other people declare a new int as -1 and set the SelectedIndex to that. But I don't really understand that solution. If someone could point me in the right direction that would be awesome. Also I am quite new to windows forms as I came from a Unity background.

Thanks

1 Answers

Apparently you want a special kind of ComboBox. In your "object oriented programming" course you learned that if you want a class similar to another class, but with some special behaviour, you should create a derived class:

class PreviousSelectedComboBox : ComboBox         // TODO: invent proper name
{
    private int previousSelectedIndex = -1;

    [System.ComponentModel.Browsable(false)]
    public virtual int PreviousSelectedIndex {get; private set;}
    {
        get => this.previousSelectedIndex;
        set => this.previousSelectedIndex = value;
    }

    [System.ComponentModel.Browsable(false)]
    public override int SelectedIndex
    {
        get => base.SelectedIndex;
        set
        {
            if (this.SelectedIndex != value)
            {
                this.PreviousSelectedIndex = this.SelectedIndex;
                base.SelectedIndex = value;

                // TODO: call OnSelectedIndexChanged?
            }
        }
    }
}

Test In a dummy test program or a unit test, check if ComboBox.OnSelectedIndexChanged is called by base.SelectedIndex, If not, call it in the SelectedIndex.Set. Also check what happens if ComboBox.SelectedItem.Set is called. Does this change the selected index by calling your overridden property SelectedIndex?

Event: I don't think that you need an event PreviousSelectedIndexChanged. It won't add anything, because this event is raised whenever event SelectedIndexChanged is raised, so those who want to get notified when PreviousSelectedIndex changes, could subsbribe to event SelectedIndexChanged.

Still, if you want such an event, follow the pattern that is used in property SelectedIndex and in OnSelectedIndexChanged.

Related