在Winform的组合框中获取旧的选定索引

问题描述 投票:6回答:3

我有一个组合框(winform)。这个组合框有一些项目(例如1,2,3,4)。

现在,当我更改此组合中的选择时,我希望知道旧索引和新索引。

我怎么得到这个?

我希望避免的可能方法。

  1. 添加一个enter事件,缓存当前索引然后选择索引更改获取新索引。
  2. 使用事件发件人收到的所选文本/所选项目属性。

我理想的想要:

  1. 在收到的args事件中,我想要的是: e.OldIndex; e.newIndex; 现在,在SelectionIndex Change事件中收到的事件参数完全没用。
  2. 我不想使用多个事件。
  3. 如果C#,不提供此功能,我可以将我的事件传递给旧索引和新索引作为事件参数吗?
c# winforms combobox
3个回答
6
投票

似乎这是一个可能的重复

ComboBox SelectedIndexChanged event: how to get the previously selected index?

内置任何内容,您将需要监听此事件并在类变量中跟踪。

但这个答案似乎提出了一种合理的方式来扩展组合框以跟踪以前的指数https://stackoverflow.com/a/425323/81053


0
投票

1 - 制作整数列表 2 - 将按钮绑定以切换到上一个屏幕(按钮名称“prevB”) 3 - 按照代码中的描述更改ComboBox索引

//initilize List and put current selected index in it

List<int> previousScreen = new List<int>();
previousScreen.Add(RegionComboBox.SelectedIndex);    

//Button Event
 private void prevB_Click(object sender, EventArgs e)
    {
        if (previousScreen.Count >= 2)
        {
            RegionComboBox.SelectedIndex = previousScreen[previousScreen.Count - 2];
        }
    }

0
投票

您需要使用以下控件替换ComboBox:

public class AdvancedComboBox : ComboBox
{
    private int myPreviouslySelectedIndex = -1;
    private int myLocalSelectedIndex = -1;

    public int PreviouslySelectedIndex { get { return myPreviouslySelectedIndex; } }

    protected override void OnSelectedIndexChanged(EventArgs e)
    {
        myPreviouslySelectedIndex = myLocalSelectedIndex;
        myLocalSelectedIndex = SelectedIndex;
        base.OnSelectedIndexChanged(e);
    }
}

现在你可以获得PreviouslySelectedIndex财产。

© www.soinside.com 2019 - 2024. All rights reserved.