Moving ListViewItems Up & Down

前端 未结 6 2879
你的背包
你的背包 2021-02-20 16:45

I have a ListView (WinForms) in which i want to move items up and down with the click of a button. The items to be moved are the ones who are checked. So if item 2, 6 and 9 are

6条回答
  •  感动是毒
    2021-02-20 17:32

    Just to complete the @Jason Larkes answer to make it support "move down" properly, add this right before the foreach in the MoveListViewItems function he provided:

    ListViewItem[] itemsToBeMoved = sender.SelectedItems.Cast().ToArray(); 
    IEnumerable itemsToBeMovedEnum;
    if (direction == MoveDirection.Down)
         itemsToBeMovedEnum = itemsToBeMoved.Reverse();
    else
         itemsToBeMovedEnum = itemsToBeMoved;
    

    and then iterate using:

    foreach (ListViewItem item in itemsTobemovedEnum)
    

    instead of the original foreach.

    Works like a charm. @EClaesson - I hope this overcomes the issue you wrote about in the comments.

提交回复
热议问题