代码之家  ›  专栏  ›  技术社区  ›  code4life

ComboBox SelectedIndexChanged事件:如何获取以前选择的索引?

  •  15
  • code4life  · 技术社区  · 14 年前

    我有一个用户控件,它有一个组合框和一个SelectedIndexChanged事件处理程序。在事件处理程序中,我需要能够知道以前选择的索引是什么…有人能给我指出正确的方向吗?

    private void cboTargetMode_SelectedIndexChanged(object sender, EventArgs e)
    {
        // need to get the previously selected index and do some handling here...
        // ... some handler code here ...
    
    
        switch (cboTargetMode.SelectedIndex)
        {
            case 1:  // ..... some code here...
                break;
            case 2:  // ..... some code here...
                break;
            case 3:  // ..... some code here...
                break;
            default: // ..... some code here...
                break;
        }
    }
    
    5 回复  |  直到 7 年前
        1
  •  23
  •   Adam Houldsworth    7 年前

    没有内置的内容,您将需要侦听此事件并在实例变量中保持跟踪。

    使用-1作为未初始化的“最后一个索引”,所以在第一次传递时,您设置它,但不要使用它。随后的过程您将使用它并设置它。

    您可以始终使用自己的派生ComboBox类和重写 OnSelectedIndexChanged 揭露一个 PreviousSelectedIndex 财产。这样,它就不会与表单紧密耦合。或者,正如您可以使用事件来完成此操作一样,它也可以作为 extender provider .

        2
  •  3
  •   Will Marcouiller    14 年前

    我想您必须将当前的(稍后将变为上一个)存储到一个变量中,这样它就可以像缓存那样使用。

    private void cboTargetMode_SelectedIndexChanged(object sender, EventArgs e) {
        // need to get the previously selected index and do some handling here...
        // ... some handler code here ...
    
        // Assuming that the variable PreviousSelectedIndex is declared in the class with value -1.
        if (PreviousSelectedIndex < 0)
            PreviousSelectedIndex = cbo.TargetMode.SelectedIndex;
        else
            // Do some handling here...
    
        switch (cboTargetMode.SelectedIndex) {
            case 1:  // ..... some code here...
                break;
            case 2:  // ..... some code here...
                break;
            case 3:  // ..... some code here...
                break;
            default: // ..... some code here...
                break;
        }
    }
    

    这是你已经想到的吗?

    否则,可能会使用 Control.Validating 事件?我不能说这件事是发生在 SelectedIndexChanged 事件。=(

        3
  •  0
  •   Zekeriyya Güçlü    8 年前

    “SelectionChangeCommitted”组合框怎么样?这是在进行选择后调用的,菜单将折叠,但项没有完全更改。所以只需阅读comboBox.selectedText或comboBox.selectedValue,甚至是comboBox.selectedIndex。

    private void comboBox1_SelectionChangeCommitted(object sender, EventArgs e)
    {
        int prevIndex = comboBox1.SelectedIndex;
    }
    
        4
  •  0
  •   Yusufm.Salh    8 年前

    这个要得到当前选定的索引,我只需要它,找不到任何地方,所以我希望它有帮助

     private void lsbx_layers_SelectedIndexChanged(object sender, EventArgs e)
            {
                int i = lsbx_layers.SelectedIndices[0];//selected index
                MessageBox.Show("Selected item at index : " + i);
            }
    
        5
  •  0
  •   Jessie Lulham    7 年前

    我有一个类似的问题,在这个问题中,我将所有组合框设置为“自动完成”,使用

    ComboBox.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
    ComboBox.AutoCompleteSource = AutoCompleteSource.ListItems;
    

    我把他们所有失去焦点的事件都循环了一遍:

    foreach(Control control in this.Controls)
    {
        if(control is ComboBox)
       {
            ((ComboBox)control).LostFocus += ComboBox_LostFocus;
       }
    }
    

    并且有一个字典对象来保存旧值

    public Dictionary<string, int> comboBoxOldValues = new Dictionary<string, int>();
    

    最后确保该值存在或设置为旧索引,然后保存到字典

    private void ComboBox_LostFocus(object sender, EventArgs e)
    {
        ComboBox comboBox = (ComboBox)sender;
    
        if (comboBox.DataSource is List<YourDataType>)
        {
            if (((List<YourDataType>)comboBox.DataSource).Count(x => x.YourValueMember == (YourValueMemberType)comboBox.SelectedValue) == 0)
            {
                if (comboBoxOldValues.Keys.Count(x => x == comboBox.Name) > 0)
                {
                    comboBox.SelectedIndex = comboBoxOldValues[comboBox.Name];
                }
                else
                    comboBox.SelectedIndex = -1;
            }
        }            
    
        if (comboBoxOldValues.Keys.Count(x => x == comboBox.Name) > 0)
        {
            comboBoxOldValues[comboBox.Name] = comboBox.SelectedIndex;
        }
        else
            comboBoxOldValues.Add(comboBox.Name,  comboBox.SelectedIndex);
    
        if (comboBox.SelectedIndex == -1)
            comboBox.Text = string.Empty;
    }