Search code examples
c#listboxlistboxitem

How do I make my listbox allow me to select more than one item


I need help to make my AdditionalStaffEmailListBox_SelectedIndexChanged allow me to select more than one item. Right now It is only allowing me to select one item.

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;
 }

Solution

  • AdditionalStaffEmailListBox.SelectionMode = SelectionMode.MultiExtended;

    Hold Ctrl and then click to select the ones you want.