Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何忽略列表框中的过度选择_C#_Listbox - Fatal编程技术网

C# 如何忽略列表框中的过度选择

C# 如何忽略列表框中的过度选择,c#,listbox,C#,Listbox,目前我有6个列表框。我正在尝试选择与手动选择的列表框项目位于同一行的所有其他5个列表框项目 但是,我得到一个错误: “System.StackOverflowException”类型的未处理异常 发生在System.Windows.Forms.dll中 无法计算表达式,因为当前线程位于堆栈中 溢出状态 下面是其中一个listbox的代码示例,对所有其他listbox重复 私有无效列表框1\u SelectedIndexChanged(对象发送方,事件参数e) { listBox1.S

目前我有6个列表框。我正在尝试选择与手动选择的列表框项目位于同一行的所有其他5个列表框项目

但是,我得到一个错误:

“System.StackOverflowException”类型的未处理异常 发生在System.Windows.Forms.dll中

无法计算表达式,因为当前线程位于堆栈中 溢出状态

下面是其中一个listbox的代码示例,对所有其他listbox重复

私有无效列表框1\u SelectedIndexChanged(对象发送方,事件参数e) {

      listBox1.SelectedIndexChanged=1;
        listBox3.SelectedIndex = -1;
        listBox2.SelectedIndex = -1;
        listBox4.SelectedIndex = -1;
        listBox5.SelectedIndex = -1;
        listBox6.SelectedIndex = -1;
        listBox2.ClearSelected();
       listBox3.ClearSelected();
       listBox4.ClearSelected();
       listBox5.ClearSelected();
       listBox6.ClearSelected();
       int  a = listBox1.SelectedIndex;
       int b = listBox2.SelectedIndex;
       int c = listBox3.SelectedIndex;
       int d = listBox4.SelectedIndex;
       int f = listBox5.SelectedIndex;
       int g = listBox6.SelectedIndex;


        if (a >-1)
        {
           // listBox1.SetSelected(a,true);
            listBox2.SetSelected(a, true);
            listBox3.SetSelected(a, true);
            listBox4.SetSelected(a, true);
            listBox5.SetSelected(a, true);
            listBox6.SetSelected(a, true);
        }
        else if (b > -1)
        {
            listBox1.SetSelected(b, true);
           // listBox2.SetSelected(b, true);
            listBox3.SetSelected(b, true);
            listBox4.SetSelected(b, true);
            listBox5.SetSelected(b, true);
            listBox6.SetSelected(b, true);
        }
        else if (c > -1)
        {
            listBox1.SetSelected(c, true);
            listBox2.SetSelected(c, true);
         //   listBox3.SetSelected(c, true);
            listBox4.SetSelected(c, true);
            listBox5.SetSelected(c, true);
            listBox6.SetSelected(c, true);
        }
        else if (d > -1)
        {
            listBox1.SetSelected(d, true);
            listBox2.SetSelected(d, true);
            listBox3.SetSelected(d, true);
          //  listBox4.SetSelected(d, true);
            listBox5.SetSelected(d, true);
            listBox6.SetSelected(d, true);
        }
        else if (f > -1)
        {
            listBox1.SetSelected(f, true);
            listBox2.SetSelected(f, true);
            listBox3.SetSelected(f, true);
            listBox4.SetSelected(f, true);
           // listBox5.SetSelected(f, true);
            listBox6.SetSelected(f, true);
        }
        else if (g > -1)
        {
            listBox1.SetSelected(g, true);
            listBox2.SetSelected(g, true);
            listBox3.SetSelected(g, true);
            listBox4.SetSelected(g, true);
            listBox5.SetSelected(g, true);
           // listBox6.SetSelected(g, true);
        }


    }


    private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
    {
        listBox1.SelectedIndex = -1;
        listBox3.SelectedIndex = -1;
        listBox4.SelectedIndex = -1;
        listBox5.SelectedIndex = -1;
        listBox6.SelectedIndex = -1;
        listBox1.ClearSelected();
        listBox3.ClearSelected();
        listBox4.ClearSelected();
        listBox5.ClearSelected();
        listBox6.ClearSelected();
        int a = listBox1.SelectedIndex;
        int b = listBox2.SelectedIndex;
        int c = listBox3.SelectedIndex;
        int d = listBox4.SelectedIndex;
        int f = listBox5.SelectedIndex;
        int g = listBox6.SelectedIndex;


        if (a > -1)
        {
            // listBox1.SetSelected(a,true);
            listBox2.SetSelected(a, true);
            listBox3.SetSelected(a, true);
            listBox4.SetSelected(a, true);
            listBox5.SetSelected(a, true);
            listBox6.SetSelected(a, true);
        }
        else if (b > -1)
        {
            listBox1.SetSelected(b, true);
            // listBox2.SetSelected(b, true);
            listBox3.SetSelected(b, true);
            listBox4.SetSelected(b, true);
            listBox5.SetSelected(b, true);
            listBox6.SetSelected(b, true);
        }
        else if (c > -1)
        {
            listBox1.SetSelected(c, true);
            listBox2.SetSelected(c, true);
            //   listBox3.SetSelected(c, true);
            listBox4.SetSelected(c, true);
            listBox5.SetSelected(c, true);
            listBox6.SetSelected(c, true);
        }
        else if (d > -1)
        {
            listBox1.SetSelected(d, true);
            listBox2.SetSelected(d, true);
            listBox3.SetSelected(d, true);
            //  listBox4.SetSelected(d, true);
            listBox5.SetSelected(d, true);
            listBox6.SetSelected(d, true);
        }
        else if (f > -1)
        {
            listBox1.SetSelected(f, true);
            listBox2.SetSelected(f, true);
            listBox3.SetSelected(f, true);
            listBox4.SetSelected(f, true);
            // listBox5.SetSelected(f, true);
            listBox6.SetSelected(f, true);
        }
        else if (g > -1)
        {
            listBox1.SetSelected(g, true);
            listBox2.SetSelected(g, true);
            listBox3.SetSelected(g, true);
            listBox4.SetSelected(g, true);
            listBox5.SetSelected(g, true);
            // listBox6.SetSelected(g, true);
        }

}

您的代码可能正在更改代码中某个SelectedIndexChanged事件中的索引

请尝试改用标志:

listBox1.SelectedIndexChanged += listBox_SelectedIndexChanged;
listBox2.SelectedIndexChanged += listBox_SelectedIndexChanged;
listBox3.SelectedIndexChanged += listBox_SelectedIndexChanged;

bool changingSelection = false;

void listBox_SelectedIndexChanged(object sender, EventArgs e) {
  if (!changingSelection) {
    int index = ((ListBox)sender).SelectedIndex;
    if (index > -1) {
      changingSelection = true;
      listBox1.SetSelected(index, true);
      listBox2.SetSelected(index, true);
      listBox3.SetSelected(index, true);
      changingSelection = false;
    }
  }
}

永远不要使用空的Try-Catch。
a+1>0
是表示
a>-1
的另一种方式吗?是否只有listBox2激发SelectedIndexChanged事件在listBox2\u SelectedIndexChanged中处理?LarsTech您是对的。谢谢大家!@对不起,我英语不好,但是如果我明白你的意思,答案是肯定的。请通过答案部分查看我的完整代码。谢谢。抱歉,我是编程新手,所以我仍在试图弄清楚您的代码中发生了什么。谢谢你的帮助。@Adrian如果你对我的例子不了解,可以随时问我。当然可以。listBox 1.SelectedIndexChanged+=listBox\u SelectedIndexChanged;listBox2.SelectedIndexChanged+=listBox\u SelectedIndexChanged;listBox3.SelectedIndexChanged+=listBox\u SelectedIndexChanged;这是干什么用的?flag和FlagsAttribute一样吗?它是什么意思/作用?该标志只是指一个布尔变量:changingSelection。如果为true,它将忽略
if
块中的代码,因为当您更改列表框的选择时,它将始终触发此代码。
listBox1.SelectedindexChange+=
stuff正在将事件分配给方法。这是一种手动连接事件的方式,而不是使用设计器。我指定每个列表框使用相同的方法。通常只分配一次事件,最好是在窗体的构造函数中。