How to remove multiple selected items in ListBox?

2019-02-17 05:45发布

My windows form contains two listboxes. Listbox1 contains some items in it and listbox2 is empty. When I press a button on the form, then multiple selected items from listbox1 should be removed from Listbox1 and copied to Listbox2.

I tried with foreach loop on listbox1.SelectedItems but it removes only 1 item from list.

Anyone has solution or code for this?

6条回答
地球回转人心会变
2楼-- · 2019-02-17 05:59
 for (int x = listBox1.SelectedIndices.Count - 1; x >= 0; x--)
        {
            int var = listBox1.SelectedIndices[x];
            listBox1.Items.RemoveAt(var);

        }

Its Works.

查看更多
Lonely孤独者°
3楼-- · 2019-02-17 05:59

This is my method:

 List<String> arr = new List<string>();
    private void btnAdd_Click(object sender, EventArgs e)
    {
        arr.Add(txtItem.Text);
        lstItem.DataSource = arr.ToArray();
        txtItem.Focus();
    }
//When i delete
    private void btnRemove_Click(object sender, EventArgs e)
    {

        foreach (string item in lstItem.SelectedItems)
        {
            arr.Remove(item);
        }
        lstItem.DataSource = arr.ToArray();

     }
查看更多
手持菜刀,她持情操
4楼-- · 2019-02-17 06:05

For VS2005 I user something similar as I couldn't use .selectedIndices

  for (int i = ListBox1.Items.Count - 1; i >= 0; i--)
        {
                if (ListBox1.Items[i].Selected)
                {
                    ListBox2.Items.Add(ListBox1.Items[i]);
                    ListBox1.Items.Remove(ListBox1.Items[i]);
                }

        }
查看更多
别忘想泡老子
5楼-- · 2019-02-17 06:13

I did this using using the CopyTo method to copy the items to an array the length of the count of selected items and then looped around that array removing each corresponding item from ListBox1.

 private void button1_Click(object sender, EventArgs e)
 {
     object[] itemsToRemove = new object[listBox1.SelectedItems.Count];
     listBox1.SelectedItems.CopyTo(itemsToRemove, 0);

     foreach (object item in itemsToRemove)
     {
         listBox1.Items.Remove(item);
         listBox2.Items.Add(item);
     }
 }
查看更多
Bombasti
6楼-- · 2019-02-17 06:22

You could do all in a single loop. You should use a simple for and loop backwards on SelectedIndices:

private void button1_Click(object sender, EventArgs e) 
{ 
    for(int x = listBox1.SelectedIndices.Count - 1; x>= 0; x--)
    { 
        int idx = listBox1.SelectedIndices[x];
        listBox2.Items.Add(listBox1.Items[idx]); 
        listBox1.Items.RemoveAt(idx);
    } 
} 
查看更多
不美不萌又怎样
7楼-- · 2019-02-17 06:22

you must store The values, you want to delete in other palce and then delete them from List,Here is sample code:

private void button1_Click(object sender, EventArgs e)
{
    ArrayList tmpArr = new ArrayList();
    foreach (object obj in listBox1.SelectedItems)
    {
        listBox2.Items.Add(obj);
        tmpArr.Add(obj);
    }
    foreach (object obj in tmpArr.ToArray())
    {
        listBox1.Items.Remove(obj);
    }
}
查看更多
登录 后发表回答