Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move items from one listbox to another

I'd like to move items from one list view to another. adding them to the second one works but the moved entries don't get removed at all.

private void MoveSelItems(ListBox from, ListBox to)
    {
        for (int i = 0; i < from.SelectedItems.Count; i++)
        {
            to.Items.Add(from.SelectedItems[i].ToString());
        }

        from.Items.Remove(to.SelectedItem);
    }

I'm using C# / Winforms / -NET 3.5

like image 709
Kai Avatar asked Nov 18 '25 19:11

Kai


1 Answers

Try this code instead at the end of the loop

foreach ( var item in new ArrayList(from.SelectedItems) ) {
  from.Items.Remove(item);
}
like image 187
JaredPar Avatar answered Nov 20 '25 09:11

JaredPar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!