0

I want to search a listbox for a object value i made. This is the override string. This is how items are added into the listbox.

 public override string ToString()
    {
        string reservatiestring;
        reservatiestring ="Kamer: " + roomNumber + "  Op datum: " + datum + "  Aantal personen: " + personen.Count + "  Naam: " + reservatienaam;
        return reservatiestring;
    }

I'd now like to search in my listbox for results while searching for a specific roomNumber. All the roomNumbers are saved in a combobox. This is what i have currently:

private void buttonSearch_Click(object sender, EventArgs e)
    {
        foreach (var item in listBox1.Items)
        {
        for (int i = listBox1.Items.Count - 1; i >= 0; i--)
        {
            if (listBox1.Items[i].ToString().ToLower().Contains(comboBox1.SelectedText.ToLower()))
            {
                listBox1.SetSelected(i, true);
            }
            else
            {
                MessageBox.Show("error");
            }
        }

This only selects one result though and its not specified to the roomNumber object only. When i put in the foreach to make it select multiple items, my program failed and i got the following error:

The list that this enumerator is bound to has been modified. An enumerator can only be used if the list is not changed

Extra info as asked for! This is where i add the info to the listbox:

private void btnReserve_Click(object sender, EventArgs e) { Reservations reservatie = new Reservations();

        reservatie.roomNumber = Convert.ToInt32(numericUpDownroom.Value);
        reservatie.datum = dateTimePicker1.Value;
        reservatie.reservatienaam = textBoxName1.Text;



        for (int i = 0; i <= personcount; i++)
        {
            Person persoon = new Person();
            persoon.naam = textBoxName1.Text;
            persoon.leeftijd = Convert.ToInt32(numericUpDownAge1.Value);
            reservatie.personen.Add(persoon);

        }
        if (!comboBox1.Items.Contains(reservatie.roomNumber))
        {
            comboBox1.Items.Add(reservatie.roomNumber);
        }
        else
            reservaties.Add(reservatie);
        listBox1.FormattingEnabled = false;
        listBox1.Items.Add(reservatie.ToString());

The error: the error when it pops up. The dutch additional information is the initial error message.

3
  • Could you share some more code or provide a more complete exception message? From the code you provided neither the comboBox or the listBox have items removed or adde, which seems to be what the message states( Changing the "Selected" property usually doesn't give this type of error. Commented Jan 20, 2017 at 20:03
  • You cannot modify the items bound to foreach inside the iteration so that's what is causing the issue but your solution should be fine with the for loop to if you have marked your selectionMode to multiple Commented Jan 20, 2017 at 20:05
  • Added more code to clarify, thanks for the fast replies! Commented Jan 20, 2017 at 20:31

1 Answer 1

0

I don't see why you need a foreach to multi select in your case especially that you are not using the "var item" anywhere in the code.

However, the exception might happen if the "SetSelected" implementation is doing some changes internally to the items.

  1. Make sure you have configured your list for multi-select

    // Set the selection mode to multiple and extended. listBox1.SelectionMode = SelectionMode.MultiExtended;

  2. Remove the foreach. (If you still need another loop, replace your foreach with a "for")

Check the below documentation link which has v. good example of multi-select ListBox: https://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.items(v=vs.110).aspx

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the suggestion, i set the selection mode. The error does however still come up. ill add a screenshot to the error message. Also what should i place in the for loop?
You must remove the foreach loop all together or replace it with a regular for loop.

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.