如何使我的列表框允许我选择多个项目

时间:2019-07-01 14:27:36

标签: c# listbox listboxitem

我需要帮助以使我的AdditionalStaffEmailListBox_SelectedIndexChanged允许我选择多个项目。现在,它只允许我选择一项。

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

1 个答案:

答案 0 :(得分:0)

AdditionalStaffEmailListBox.SelectionMode = SelectionMode.MultiExtended;

按住Ctrl,然后单击以选择所需的内容。