none
How do I allow user to select more than one item from listbox? RRS feed

  • Question

  • What I am trying to do is select items (name) from the listbox which I already have populated with 15 items (names).

    I want to select one or more names from the list. If I can get the selection of more than one item (name) from the list, then I want to send email to each item (person) selected in the listbox. Send of email is done by another process. A button Email. Right now I am not worried about that part. I just want to be able to select more than one name from the populated listbox.

    Below I have added code for PopulateEdditionalStaffEmailListBox. I hope this will help those who are trying to help me understand what I am doing. I am trying my best to make it clear as to what I am trying to achieve.

    Here is the listbox where I am trying to select more than one item. That part is not working

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

    Here is the code for populating the listbox where I am trying to select more than one name. This have worked and the listbox shows 15 items (names)

    public async void PopulateAdditionalStaffEmailListBox()
    {
        List<GetRequestorInfoModel> requestors = new List<GetRequestorInfoModel>();
        try
        {
            requestors = await FTACaseReset.Controllers.RequestorInfoController.GetAllRequestorInfoes();
            requestors = requestors.OrderBy(x => x.DisplayName).ToList(); //Has 15 items
                  
            //Populate AdditionalStaffEmailListBox
            for (int i = 0; i < requestors.Count; i++)
            {
                ListBoxItem requestor = new ListBoxItem();
                requestor.Text = requestors[i].DisplayName;
                requestor.Value = requestors[i].RequestorInfoID;
                //AdditionalStaffEmailListBox.Items.Add(i);
                AdditionalStaffEmailListBox.Items.Add(requestor.Text).ToString();
            }
        }
        catch (Exception ex)
        {
            string errorMsg = string.Format("An error has occured in {0}. \nException:\n{1}", "AdditionalStaffEmailListBox()", ex.Message);
            MessageBox.Show(errorMsg, "Application Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }


    • Edited by winkimjr2 Monday, July 8, 2019 3:14 PM I have added code that populates my listbox
    Monday, July 1, 2019 2:07 PM

All replies

  • Hello,

    You need to set up the SelectionMode property:

    // Set the SelectionMode to select multiple items.
    listBox.SelectionMode = SelectionMode.MultiExtended;


    profile for Eugene Astafiev at Stack Overflow, Q&A for professional and enthusiast programmers

    Monday, July 1, 2019 2:21 PM
  • Hello Eugene. Thanks for your help. I did make the change you suggested. However when I run the code in debug mode, I am still only able to select one item from the list. It is frustrating.
    Monday, July 1, 2019 3:35 PM
  • How does your code looks like now?

    profile for Eugene Astafiev at Stack Overflow, Q&A for professional and enthusiast programmers

    Monday, July 1, 2019 8:03 PM
  • Here is my code

    private void AdditionalStaffEmailListBox_SelectedIndexChanged(object sender, EventArgs e)
            {
                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;
            }

    Monday, July 1, 2019 8:21 PM
  • Thank you for sharing your code, but I still see the following line:

    AdditionalStaffEmailListBox.SelectionMode = SelectionMode.MultiSimple;

    But you really need:

    AdditionalStaffEmailListBox.SelectionMode = SelectionMode.MultiExtended;


    profile for Eugene Astafiev at Stack Overflow, Q&A for professional and enthusiast programmers

    Monday, July 1, 2019 8:56 PM
  • I have replace the line of code with this one 

    AdditionalStaffEmailListBox.SelectionMode = SelectionMode.MultiExtended;

    However, I have 15 items in the list. When I click run in debug mode,

    the loop seem not to ever end. And there app screen gets disabled so I cannot scroll on the listbox. It's very frustrating.

    Monday, July 1, 2019 9:14 PM
  • Hello,

    Do you call the EndUpdate method of the ListBox class after making your changes?

    public void AddToMyListBox()
    {
       // Stop the ListBox from drawing while items are added.
       listBox1.BeginUpdate();
    
       // Loop through and add five thousand new items.
       for(int x = 1; x < 5000; x++)
       {
          listBox1.Items.Add("Item " + x.ToString());   
       }
       // End the update process and force a repaint of the ListBox.
       listBox1.EndUpdate();
    }
    


    profile for Eugene Astafiev at Stack Overflow, Q&A for professional and enthusiast programmers

    Wednesday, July 3, 2019 6:29 PM
  • Eugene, can you please review the code I added and see if this helps you understand what I am trying to do? The code is the one that populates the listbox. This code is working because the listbox gets populated with 15 names. It is selecting more than one person from the populated listbox that is not working. The listbox gets names from the database using these two lines of code.

    requestors = await FTACaseReset.Controllers.RequestorInfoController.GetAllRequestorInfoes();
    requestors = requestors.OrderBy(x => x.DisplayName).ToList(); //Has 15 items

    Monday, July 8, 2019 3:18 PM