如何在两个(多个)列表框中选择相同的索引

本文关键字:选择 索引 多个 两个 列表 | 更新日期: 2023-09-27 17:50:12

看,我有两个listboxes: listBox1, listBox2,,它们都有多个选择,这意味着每个listbox中可以单独选择的项目很少。

我需要为此制作一个方法或smth:

如果在一个listbox中选择项目,则必须在另一个CCD-3中自动选择具有相同索引的项目,反之亦然。

我知道如何通过一个选择来管理它。

请帮忙!

如何在两个(多个)列表框中选择相同的索引

我不知道您的确切场景,但您可以使用此解决方案在两个ListBoxes 中选择相同的索引

private void button1_Click(object sender, System.EventArgs e)
{
    int index;//Your Index select in any listbox
    listBox1.SetSelected(index,true);
    listBox2.SetSelected(index,true);
}

假设您有n个列表框:

List<ListBox> MyList=new List<ListBox>();
  int index;
Mylist.add(ListBox1);
MyList.add(ListBox2);
....
private void ApplyIndex(int index)
{
   foreach(ListBox li in MyList)
     {
         li.SelectedIndex=index;      
      }
}

对于每个ListBox,我添加一个selection_change事件以获得索引:

  private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
           int x= listBox1.SelectedIndex;
            ApplyIndex(x);
         }
private void listBox2_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
           int x= listBox2.SelectedIndex;
            ApplyIndex(x);
         }
......

如果Winforms,这里有一个小类可以完成任务(未经测试(。

class SelectionIndexDifference {
    List<int> previousIndices = new List<int>();
    public event Action<int, bool> SelectionChanged;
    public void SetSelectionChange(SelectedIndexCollection selectedIndices) {
        var newIndices = new List<int> ();
        for (int i = 0; i < selectedIndices.Count; i++) {
            newIndices.Add ((int)selectedIndices [i]);
        }
        var addedIndices = new SortedSet<int> (newIndices);
        addedIndices.ExceptWith (previousIndices);
        var removedIndices = new SortedSet<int> (previousIndices);
        removedIndices.ExceptWith (newIndices);
        foreach (var index in addedIndices) {
            SelectionChanged (index, true);
        }
        foreach (var index in removedIndices) {
            SelectionChanged (index, false);
        }
        previousIndices = newIndices;
    }
}

在表单中,您可以创建此类的实例作为字段(称为selectDiff(,在构造函数中(初始化组件后(,您可以注册事件。

partial class FormX {
    SelectionIndexDifference selectDiff = new SelectionIndexDifference ();
    public FormX() {
        InitializeComponent ();
        listBox1.SelectedIndexChanged += (sender, e) => {
            selectDiff.SetSelectionChange (listBox1.SelectedItems);
        };
        listBox2.SelectedIndexChanged += (sender, e) => {
            selectDiff.SetSelectionChange (listbox2.SelectedItems);
        };
        selectDiff.SelectionChanged += listBox1.SetSelected;
        selectDiff.SelectionChanged += listBox2.SetSelected;
    }
}

编辑:

您可以通过删除更新从用户输入中更改选择的列表框的需要来提高效率-通过具有SelectIndexDifference的多个实例(每个列表框一个(,其中您不将相应的列表框订阅到SelectionChanged,例如,如果我们保持3个列表框同步:

listBox1.SelectedIndexChanged += (sender, e) => {
    selectDiff1.SetSelectionChange (listBox1.SelectedItems);
};
selectDiff1.SelectionChanged += listBox2.SetSelected;
selectDiff1.SelectionChanged += listBox3.SetSelected;

listBox2.SelectedIndexChanged += (sender, e) => {
    selectDiff2.SetSelectionChange (listbox2.SelectedItems);
};
selectDiff2.SelectionChanged += listBox1.SetSelected;
selectDiff2.SelectionChanged += listBox3.SetSelected;

listBox3.SelectedIndexChanged += (sender, e) => {
    selectDiff3.SetSelectionChange (listBox1.SelectedItems);
};
selectDiff3.SelectionChanged += listBox1.SetSelected;
selectDiff3.SelectionChanged += listBox2.SetSelected;

然而,这将消耗N倍的内存,因为"previousIndices"是为每个实例存储的,尽管它们应该始终具有相同的索引。可以改进这一点,将其存储一次,并且仍然有多个类似于事件的实例。我把它留给你练习。