首页 文章

C#在多个listBox上选择相同的索引

提问于
浏览
0

好的,所以我有四个listBox控件 . 我想在所有四个listBox上选择相同的索引,当它们中的任何一个被点击时 . 要提到的是,我确实在程序中更改了索引 . 我尝试使用方法listSelectChange(int index)并为每个listBox添加selectIndexChange的事件,但即使select由程序而不是用户控制,它也会激活该事件 .

请不要使用课程,只需一个粗暴的方法就可以了!

1 回答

  • 0

    您可以在更新ListBox之前取消订阅selectedIndexChanged,然后立即重新订阅它 . 这是一种常见的做法 .

    既然你没有给出任何代码示例,我在这里做一些猜测 .

    // Enumerable of all the synchronized list boxes
    IEnumerable<ListBox> mListBoxes = ...
    ...
    public void OnSelectedIndexChanged(object sender, EventArgs e) {
        var currentListBox = (ListBox)sender;
    
        // Do this for every listbox that isn't the one that was just updated
        foreach(var listBox in mListBoxes.Where(lb => lb != currentListBox)) {
            listBox.SelectedIndexChanged -= OnSelectedIndexChanged;
            listBox.SelectedIndex = currentListBox.SelectedIndex;
            listBox.SelectedIndexChanged += OnSelectedIndexChanged;
        }
    }
    

相关问题