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;
 }
 
C#:
private void AdditionalStaffEmailListBox_SelectedIndexChanged(object sender, EventArgs e)
              {
              foreach (string str in AdditionalStaffEmailListBox.SelectedItems)
                           {
                           AdditionalStaffEmailListBox.Items.Add(str); /* Add the items that are selected */
                           }
              }
That code doesn't make sense. With the listbox you just selected from, you add more listbox item(s) to the same listbox. I can't see any possible reason why you would want to do that. It would make more sense to maybe add the items from the collection you are currently looping and add them to a new totally different listbox's collection. Which is what my example does. Regardless however you want to do it, your answer for doing it correct is on post 12.
 
Back
Top Bottom