How to I make my listbox allow me to select as may items as I want?

okaymy1112

Member
Joined
Feb 27, 2019
Messages
8
Programming Experience
1-3
My code for The following code for AdditionalStaffEmailListBox_SelectedIndexChanged listbox is not allowing me to select more than one item from the listbox.
How do I change it so I am able to select as many items from the listbox as I want?

C#:
private void AdditionalStaffEmailListBox_SelectedIndexChanged(object sender, EventArgs e)
{
    AdditionalStaffEmailListBox = new ListBox();
    AdditionalStaffEmailListBox.SelectionMode = SelectionMode.MultiSimple;
    AdditionalStaffEmailListBox.BeginUpdate();

    //Loop through all items in the AdditionalStaffEmailListBox
    for (int x = 0; x < AdditionalStaffEmailListBox.Items.Count; x++)
    {
        //AdditionalStaffEmailListBox.Items.Add("Item " + x.ToString());
        if (AdditionalStaffEmailListBox.GetSelected(x) == true)
        {
        //Deselect all items that are selected
        AdditionalStaffEmailListBox.SetSelected(x, false);
        }
        else
        {
        //Select all items that are not selected
         AdditionalStaffEmailListBox.SetSelected(x, true);
        }
    }
    //Force the AdditionalStaffEmailListBox to scroll back to the top of the list
    AdditionalStaffEmailListBox.TopIndex = 0;
 }
 
Whoa! Why are you creating a new ListBox in that event handler? AdditionalStaffEmailListBox already refers to the control that was created when the form was created. Why do you want to now create a new one?
 
Whoa! Why are you creating a new ListBox in that event handler? AdditionalStaffEmailListBox already refers to the control that was created when the form was created. Why do you want to now create a new one?
I have removed the code for creating new listbox. However I now get
System.StackOverflowException​
 
Last edited:
Why did you change the contents of what you asked on post 4? Please do not edit your posts in whole, instead reply below what you wrote. Nobody knows what I was recommending you to delete now...
 
Why did you change the contents of what you asked on post 4? Please do not edit your posts in whole, instead reply below what you wrote. Nobody knows what I was recommending you to delete now...
I did not delete anything. This line of code is still in the code
AdditionalStaffEmailListBox = new ListBox();
 
Yes you did. The moderator in the topic will confirm it when he checks back in. On post 5 I replied to you advising you on what to delete, but you changed the content of the post almost immediately after posting. I'm not on about your code, I am on about the forth post which you edited after posting.

As for your question, have you looked into the MSDN docs on replacing the method above with the selecteditems property?
 
Whoa! Why are you creating a new ListBox in that event handler? AdditionalStaffEmailListBox already refers to the control that was created when the form was created. Why do you want to now create a new one?
I really appreciate your help. I know you do not have to try to help me but you are patient with me. I think I am just a little slow. I did check the link you gave me.
public System.Windows.Forms.ListBox.SelectedObjectCollection SelectedItems { get; } However, I am not sure how to replace my method with selecteditems property. I am sure it is simpler than I think.
 
It is simpler than you think and I already told you how to go about it above.
You need to use the selected items property and iterate its collection of whatever objects you have added to it.
Loop through the collection of selected items with a for each loop and while inside that loop, with each selected item, you do what you want with that item. Try it and post back if you get stuck. We're always around to help out. Just show what you tried if it doesn't work. :)
 
I am guessing that part of the problem that the OP is having is that he is trying to process the selected items while he is in his SelectedIndexChanged event handler. He should really process the selected items when the user finalizes his selection by clicking his "Send" or "OK" button. I'm assuming there is a "Send" button because of his other thread where he was asking about how to have listbox that contains other recipients.
 
Yes, you really don't want these checks firing every time you select some new item from the list, maybe this will help OP to get started...
Populate list (You can ignore this if you have items in your list):
            var i = 0;
            do
            {
                listBox1.Items.Add(string.Concat("Item ", i));
                i++;
            } while (i < 10);
You will also want to make sure Multiselect is allowed by setting this property somewhere :: listBox1.SelectionMode = SelectionMode.MultiExtended;

567


Selected Items:
            foreach (string str in listBox1.SelectedItems)
            {
                listBox2.Items.Add(str); /* Add the items that are selected */
            }
And to get the non selected items if you need them ::
Non selected items:
            foreach (string str in listBox1.Items)
            {
                if (!listBox1.SelectedItems.Contains(str)) /* It's not a selected item */
                {
                    listBox3.Items.Add(str);
                }
            }
If you look at it, you can do it all with only one loop.

Told you it was simple. Dump the code into a button and run it. Providing you change the names of the listboxes in this, to your actual names set in your project, it will work.
 
I am guessing that part of the problem that the OP is having is that he is trying to process the selected items while he is in his SelectedIndexChanged event handler. He should really process the selected items when the user finalizes his selection by clicking his "Send" or "OK" button. I'm assuming there is a "Send" button because of his other thread where he was asking about how to have listbox that contains other recipients.
Yes you are 100% correct. After the user selects names from the listbox, there is a Email button that they will click to send email to each selected user. I will now try what was suggested and post back. I am very happy that each of you are very patient, understanding and willing to help. You 2 are great human beings. The world (people like me) needs you!
 
Yes, you really don't want these checks firing every time you select some new item from the list, maybe this will help OP to get started...
Populate list (You can ignore this if you have items in your list):
            var i = 0;
            do
            {
                listBox1.Items.Add(string.Concat("Item ", i));
                i++;
            } while (i < 10);
You will also want to make sure Multiselect is allowed by setting this property somewhere :: listBox1.SelectionMode = SelectionMode.MultiExtended;

View attachment 567

Selected Items:
            foreach (string str in listBox1.SelectedItems)
            {
                listBox2.Items.Add(str); /* Add the items that are selected */
            }
And to get the non selected items if you need them ::
Non selected items:
            foreach (string str in listBox1.Items)
            {
                if (!listBox1.SelectedItems.Contains(str)) /* It's not a selected item */
                {
                    listBox3.Items.Add(str);
                }
            }
If you look at it, you can do it all with only one loop.

Told you it was simple. Dump the code into a button and run it. Providing you change the names of the listboxes in this, to your actual names set in your project, it will work.
My selectedItem is found inside AdditionalStaffEmailListBox.Items. For example I have a name selected Jim Guy showing as in
AdditionalStaffEmailListBox.Items {SelectedItem = "Guy, Jim"}.
I am getting System.InvalidOperationException: 'List that this enumerator is bound to has been modified. An enumerator can only be used if the list does not change.'
Here is my new code
When I run the code when a sender is selected from the listbox sender appear like this in debug mode
sender{SelectedItem = "Guy, Jim"}
private void AdditionalStaffEmailListBox_SelectedIndexChanged(object sender, EventArgs e)
{
foreach (string str in AdditionalStaffEmailListBox.SelectedItems)
{
AdditionalStaffEmailListBox.Items.Add(str); /* Add the items that are selected */
}
}
 
Last edited:

Latest posts

Back
Top Bottom