Windows窗体下拉如何获取以前选择的值

时间:2014-01-23 12:00:33

标签: winforms c#-3.0

我来自网络背景,现在正在开发一个Windows应用程序。

应用程序已下拉,显示所选项目(来自数据库)。我要求在选择除现有值之外的任何其他值时执行xyz业务逻辑。

我可以使用索引更改事件处理程序,但如果他们选择其他项并选择返回相同,我不想执行xyz业务逻辑。

因此,有人可以帮助您如何将所选值与加载时选择的值进行比较吗?

我可以有会话来存储以前的状态,但不确定我们如何在Windows窗体中执行相同操作。

1 个答案:

答案 0 :(得分:0)

在这种情况下,您的会话存储概念是本地存储变量。示例类:

public class MyForm : Form
{
    private Int32 _selIdx = -1;  //this is local to the Form and accessible anywhere within the MyForm class

    public MyForm() { }

    private void MyForm_Load(Object sender, EventArgs e)
    {
        //load database data, add to combo box, then capture the index

        _selIdx = myComboBox.SelectedIndex;
    }

    //I prefer this method because it reacts to user interaction, and not programmatic change of index value
    private void myComboBox_SelectionChangeCommitted(Object sender, EventArgs e)
    {
        //index changed, react
        if (!_selIdx.Equals(myComboBox.SelectedIndex))
        {
            //not a match, do something...
        }
    }
}
相关问题