Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to move a listview item up or down with a button click

I have items in a listview and I want to know how to add 2 buttons one that makes the item move up in the list and one that make the item move down in the list. I figure out how to do it with listboxes but I cant figure out or find where to do it for list views and help is appreciated.

like image 738
Ian Lundberg Avatar asked Feb 14 '12 04:02

Ian Lundberg


1 Answers

Check out the Listview.Items.Remove and Listview.Items.Insert methods:

listView1.Items.Remove(selectedItem);
listView1.Items.Insert(newIndex, selectedItem);

Dealing with the changing index (when you remove a ListViewItem, the indexes of the list items which come after the selected item all change by one) can be a pain. Also, you will need to manage what happens when you promote or demote an item past the top or lower bounds of the item indexes.

Hope that gets you started.

like image 99
XIVSolutions Avatar answered Sep 20 '22 20:09

XIVSolutions