Results 1 to 5 of 5

Thread: [RESOLVED] multiple items in a listbox

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2005
    Posts
    17

    Resolved [RESOLVED] multiple items in a listbox

    I am trying to remove multiple selected items in a listbox, this code isnt working:
    Code:
    private void btnRemove_Click(object sender, System.EventArgs e)
    {
    	for (int x = 0; x < lstFiles.Items.Count; x++)
    	{
    		if (lstFiles.SelectedIndices.Contains(x))
    		{
    			lstFiles.Items.RemoveAt(x);
    		}
    	}
    }
    Not all selected items get removed, but most of them do.
    edit: seems its the first thing selected that doesnt get removed.
    Last edited by Triumph; Nov 6th, 2005 at 11:44 AM.

  2. #2
    Lively Member deranged's Avatar
    Join Date
    Jun 2004
    Location
    TN
    Posts
    104

    Re: multiple items in a listbox

    Try this:

    Code:
    private void btnRemove_Click(object sender, System.EventArgs e)
    {
    	for (int x = 0; x != lstFiles.Items.Count; x++)
    	{
    		if (lstFiles.SelectedIndices.Contains(x))
    		{
    			lstFiles.Items.RemoveAt(x);
    		}
    	}
    }

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Oct 2005
    Posts
    17

    Re: multiple items in a listbox

    i tried that, and it has the same exact problem, first selected item doesnt get removed.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: multiple items in a listbox

    If you want to remove multiple items from a collection then you should start from the end and work backwards so that each item you remove does not affect the remaining indices:
    Code:
    private void btnRemove_Click(object sender, System.EventArgs e)
    {
    	for (int x = lstFiles.Items.Count - 1; x >= 0; lstFiles.Items.Count; x--)
    	{
    		if (lstFiles.SelectedIndices.Contains(x))
    		{
    			lstFiles.Items.RemoveAt(x);
    		}
    	}
    }
    but in this case you could actually do this:
    Code:
    while (this.listBox1.SelectedItems.Count > 0)
    {
        this.listBox1.Items.Remove(this.listBox1.SelectedItems[0]);
    }
    or this:
    Code:
    for (int i = this.listBox1.SelectedItems.Count - 1; i >= 0; i--)
    {
        this.listBox1.Items.Remove(this.listBox1.SelectedItems[i]);
    }
    Last edited by jmcilhinney; Nov 6th, 2005 at 06:31 PM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Oct 2005
    Posts
    17

    Re: multiple items in a listbox

    Thanks alot. That fixed it. I understand now why it wasnt removing the right items, I never considered the fact that the indices changed as things got removed.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width