Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My code here is not allowing me to select more than one item from list box. When I select an item in debug mode and click run, when I click on another item, the item I selected last is no longer selected.
I need help figure out why this is not allowing me to select as many items as I want.

I have added code to show how I have populated my listbox. It is this populated listbox I am trying to select more than one item (name) so I can eventually send an email to those selected names. For now I am not worried about sending email part. I just want to be able to select more than one name from the populated listbox.

What I have tried:

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


I already have a listbox populated (with names). Here is the listbox populated

C#
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);
    }
}
Posted
Updated 8-Jul-19 4:58am
v3

Set the selection mode once - in the designer is good - and leave it alone.
 
Share this answer
 
Comments
Member 11403304 8-Jul-19 10:44am    
How do I set selection mode once in the designer?
OriginalGriff 8-Jul-19 11:19am    
Select the control ... Properties pane: SelectionMode open the dropdown.
Take a look at your code:
C#
private void AdditionalStaffEmailListBox_SelectedIndexChanged(object sender, EventArgs e)
{
    AdditionalStaffEmailListBox = new ListBox();
    //skipped lines here
}


Inside of SelectedIndexChanged event you are creating new ListBox under the variable name equals to existing object. Newly created object is never used. It's invisible to user too.

It's not clear what you are trying to achieve...
 
Share this answer
 
Comments
Member 11403304 8-Jul-19 10:33am    
What I am trying to do is select items (name) from the listbox which I already have populated with 15 names. The user will select one or more names from the list. If I can get the selection of more than one name from the list, then I want to send email to each person selected in the listbox. Send of email is done by another process. A button Email
Maciej Los 8-Jul-19 11:46am    
So, you have to disable SelectedIndexChanged for AdditionalStaffEmailListBox. All what you need is to loop through the collection of selected items in a list inside "Send emails" method.
I'm not sure why you're initiating a new ListBox for your existing one, because that'll give you an empty ListBox.
 
Share this answer
 
v4
Comments
Member 11403304 3-Jul-19 15:42pm    
I have modified my code removing new listbox but I am still not able to select more than one item from the list box

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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900