ComboBox 的 SelectedIndexChanged 事件:如何获取之前选定的索引?

19

我有一个用户控件,其中包含一个ComboBox和一个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;
    }
}
6个回答

26

没有内置的功能,您需要监听此事件并在实例变量中跟踪它。

使用-1作为未初始化的“上一个索引”,因此在第一次传递时设置它但不使用它。后续传递中,您将使用它并设置它。

您可以始终使用自己的派生ComboBox类来执行此操作,并覆盖OnSelectedIndexChanged并公开PreviousSelectedIndex属性。这样,它就不会与表单紧密耦合。或者,由于可以使用事件执行此操作,因此也可以作为扩展提供程序实现。


应该是“实例变量”而不是“类变量”,对吗? - Frank Schmitt
@FrankSchmitt 追逐一个徽章,挖掘近8年前的帖子?;-) 但是你是正确的,我已经修改了我的答案。 - Adam Houldsworth
@AdamHouldsworth 谢谢。不过,目前我并没有徽章收集的计划 :-) - Frank Schmitt

3

我想你需要将当前值(稍后会变成之前的值)存储到一个变量中,以便像缓存一样使用。

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事件之前还是之后发生。=(


0

comboBox_SelectionChangeCommitted事件怎么样?这个事件在选择完成后被调用,菜单会折叠但选项并没有完全改变。所以只需读取comboBox.SelectedText或comboBox.SelectedValue甚至comboBox.SelectedIndex。

private void comboBox1_SelectionChangeCommitted(object sender, EventArgs e)
{
    int prevIndex = comboBox1.SelectedIndex;
}

@Zekeriya,这是6年前的事情了,但我通过双向绑定“SelectedItem”并在视图模型上绑定属性的setter中管理先前的选择来解决了问题。说实话,这证明是更好的方法,因为它更容易做到,并且不受控件的影响--这意味着我不需要为每个不同的绑定列表的控件编写一些处理程序代码。 - code4life
好的,上个月我需要解决这个问题,但是被接受的答案对我没有用。以某种方式,你现在的回复也不起作用,但我稍后会检查它...无论如何,感谢你的回复 :) - Zekeriyya Güçlü
我们目前在我的店所使用的MVVM理念是要避免转换器,避免行为(除非绝对必要),并尝试让ViewModel做更多的工作 - 即让提供SelectedItem等内容。 - code4life

0

我曾经遇到过类似的问题,我使用以下方式将所有组合框设置为“自动完成”:

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;
}

0

这个用来获取当前选定的索引,我只需要它,但是我找不到任何地方,所以希望它有所帮助。

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

0

您可以在DropDown事件中获取并存储旧值。然后在SelectionChangeCommitted事件中恢复该旧值(如果需要)。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接