简体   繁体   English

从2个列表框中选择镜像

[英]Mirror selection from 2 listbox

I have 2 Listboxes , each are on different tab page 我有2个列表框,每个列表框都在不同的标签页上

listBox1 with items A,B,C and listBox2 with exactly same items A,B,C 具有项目A,B,C的listBox1和具有完全相同的项目A,B,C的listBox2

When I select Item A from listBox1, I want Item A from listBox2 selected aswell and vice versa 当我从listBox1中选择项A时,我也希望从listBox2中选择项A,反之亦然

I use this code : 我使用此代码:

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

            string item = listBox1.SelectedItem.ToString();
            int index = listBox2_Fichiers.FindString(item);
            listBox2.SetSelected(index, true);
}
private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
        {

            string item = listBox2.SelectedItem.ToString();
            int index = listBox1_Fichiers.FindString(item);
            listBox1.SetSelected(index, true);
}

It works only in one way, from 1 to 2 or from 2 to 1 , but when I try to activate both I get this exception: System.StackOverflowException 它仅以一种方式工作,从1到2或从2到1,但是当我尝试同时激活两者时,出现以下异常:System.StackOverflowException

What am I missing ? 我想念什么?

It is because everytime you call SetSelected , SelectedIndexChanged can be called. 这是因为每次调用SetSelected ,都可以调用SelectedIndexChanged

This creates an infinite calling of listBox1.SetSelected > listBox1_SelectedIndexChanged > listBox2.SetSelected > listBox2_SelectedIndexChanged > listBox1.SetSelected > ... . 这将创建对listBox1.SetSelected > listBox1_SelectedIndexChanged > listBox2.SetSelected > listBox2_SelectedIndexChanged > listBox1.SetSelected > ...的无限调用。

Eventually, system stops you by throwing a StackOverflowException . 最终,系统通过抛出StackOverflowException阻止您。

private bool mirroring = false;

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    if (mirroring) return;
    mirroring = true;

    string item = listBox1.SelectedItem.ToString();
    int index = listBox2_Fichiers.FindString(item);
    listBox2.SetSelected(index, true);

    mirroring = false;
}
private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
{
    if (mirroring) return;
    mirroring = true;

    string item = listBox2.SelectedItem.ToString();
    int index = listBox1_Fichiers.FindString(item);
    listBox1.SetSelected(index, true);

    mirroring = false;
}

It is your responsibility to break the call chain. 打破电话链是您的责任。 Simplest way is using a boolean switch. 最简单的方法是使用布尔开关。

System.StackOverflowException 

exception occurs when you are trying to create a loop of operation. 尝试创建操作循环时发生异常。 You are changing list1 from list2's listBox2_SelectedIndexChanged event so it changes list1's index which fire list1's listBox1_SelectedIndexChanged event which again fire list2's same as before. 您正在从list2的listBox2_SelectedIndexChanged事件中更改list1,因此它将更改将触发list1的listBox1_SelectedIndexChanged事件的list1的索引,该事件再次触发list2与以前相同。 So this thing creating a loop of selected index change event and System.StackOverflowException exception thrown. 因此,这件事创建了一个选定索引更改事件和System.StackOverflowException exception引发的循环。 You have to change this event handling to prevent this 您必须更改此事件处理以防止这种情况

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM