0

Currently I have 6 listbox. Im trying to select all 5 other listbox items in the same row as the one manually selected.

However, Im getting an error :

An unhandled exception of type 'System.StackOverflowException' occurred in System.Windows.Forms.dll

Cannot evaluate expression because the current thread is in a stack overflow state.

Below is an example of code for one of the listbox, repeated for all other listbox.

private void listBox1_SelectedIndexChanged(object sender, EventArgs 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);
        }

}

4
  • Never use an empty Try-Catch. Is a + 1 > 0 another way of saying a > -1? Commented Mar 11, 2016 at 18:10
  • do you have only listBox2 fires SelectedIndexChanged event which handled in listBox2_SelectedIndexChanged? Commented Mar 11, 2016 at 18:11
  • @LarsTech Your're right. Thank you! Commented Mar 11, 2016 at 21:52
  • @skalinkin Sorry Im not strong in english, but if i get what you meant, the answer is yes. Please take a look at my full code via the answer section. Thanks Commented Mar 11, 2016 at 21:55

1 Answer 1

0

Your code is probably changing the index within the SelectedIndexChanged event somewhere in your code.

Try using a flag instead:

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;
    }
  }
}
Sign up to request clarification or add additional context in comments.

10 Comments

Hi. Apologies, Im new to programming so Im still trying to figure out what is going on in your code. Thanks for your help.
@Adrian Feel free to ask me anything you don't understand about my example.
Sure. listBox1.SelectedIndexChanged += listBox_SelectedIndexChanged; listBox2.SelectedIndexChanged += listBox_SelectedIndexChanged; listBox3.SelectedIndexChanged += listBox_SelectedIndexChanged; whats does this do ?
is flag the same as FlagsAttribute ? What does it mean/do?
The flag is just referring to a boolean variable: changingSelection. When its true, it ignores the code inside the if block, because when you change the selection of a ListBox, it will always fire this code. The listBox1.SelectedindexChange += stuff is assigning the event to the method. This is a manual way of wiring up the events instead of using the designer. I assign each of the ListBoxes to use the same method. Usually you only assign the event once, preferably in the form's constructor.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.