简体   繁体   English

C# Windows Forms - 如何在 Combobox 中获取上一个 SelectedItem

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

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.我目前正在尝试弄清楚如何在 Combobox 中获取以前的 Selected Item,数据被添加到 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.如果用户在 combobox 中选择了一个选项,则 selectedPrice int 会增加。 I am trying to take away the previousItemCost and im having trouble understanding how to find the previous selected user input.我试图拿走 previousItemCost 并且我无法理解如何找到以前选择的用户输入。

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.我不太确定如何处理这个问题,我看到其他几个人将一个新的 int 声明为 -1 并将 SelectedIndex 设置为该值。 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.我对 windows forms 也很陌生,因为我来自 Unity 背景。

Thanks谢谢

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:显然你想要一种特殊的 ComboBox。在你的“面向对象编程”课程中你了解到,如果你想要一个类似于另一个 class 的 class,但有一些特殊的行为,你应该创建一个派生的 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 .测试在虚拟测试程序或单元测试中,检查ComboBox.OnSelectedIndexChanged是否被base.SelectedIndex调用,如果没有,则在SelectedIndex.Set中调用它。 Also check what happens if ComboBox.SelectedItem.Set is called.还要检查如果ComboBox.SelectedItem.Set会发生什么。 Does this change the selected index by calling your overridden property SelectedIndex?这是否会通过调用重写的属性 SelectedIndex 来更改选定的索引?

Event : I don't think that you need an event PreviousSelectedIndexChanged.事件:我认为您不需要事件 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.它不会添加任何内容,因为每当引发事件 SelectedIndexChanged 时都会引发此事件,因此那些希望在 PreviousSelectedIndex 更改时得到通知的人可以订阅事件 SelectedIndexChanged。

Still, if you want such an event, follow the pattern that is used in property SelectedIndex and in OnSelectedIndexChanged.不过,如果您需要此类事件,请遵循属性 SelectedIndex 和 OnSelectedIndexChanged 中使用的模式。

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.我目前正在尝试弄清楚如何在 Combobox 中获取上一个选定项目,数据被添加到 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.如果用户在 combobox 中选择了一个选项,则 selectedPrice int 会增加。 I am trying to take away the previousItemCost and im having trouble understanding how to find the previous selected user input.我正在尝试删除 previousItemCost 并且我无法理解如何找到先前选择的用户输入。

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.我不太确定如何解决这个问题,我看到其他几个人将新的 int 声明为 -1 并将 SelectedIndex 设置为该值。 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.此外,由于我来自 Unity 背景,因此我对 windows forms 还是很陌生。

Thanks谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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