|
-
November 10th, 2011, 11:07 AM
#1
[RESOLVED] Multiple Listviews - Drag and Drop / Reordering
I'm wanting to drag and drop, between listviews, and allow re-ordering. Currently, I can drag and drop items to an alternate listview, and can re-order within the same, however, when I drag and drop on the new list, due to my
Code:
listView2.Items.Add(lvi);
The ListViewItem is getting added to the bottom of the list. How do I end up finding the index, so that I can use the listView2.Items.Insert, and placing at the existing index, if there is one? It's probably easy, but I can't seem to get it right. All help is appreciated.
Thanks,
Quinn
Code:
privatevoid listView2_DragDrop(object sender, DragEventArgs e)
{
ListView.SelectedListViewItemCollection myList = this.listView1.SelectedItems;
foreach (ListViewItem myItem in myList)
{
ListViewItem lvi = newListViewItem(text: myItem.Text, imageKey: myItem.ImageKey);
lvi.SubItems.Add(myItem.SubItems[1]);
lvi.SubItems.Add(myItem.SubItems[2]);
lvi.SubItems.Add(myItem.SubItems[3]);
listView1.Items.Remove(myItem);
listView2.Items.Add(lvi);
}
//Return if the items are not selected in the ListView control.
if (listView2.SelectedItems.Count == 0)
{
return;
}
//Returns the location of the mouse pointer in the ListView control.
Point cp = listView2.PointToClient(newPoint(e.X, e.Y));
//Obtain the item that is located at the specified location of the mouse pointer.
ListViewItem dragToItem = listView2.GetItemAt(cp.X, cp.Y);
if (dragToItem == null)
{
return;
}
//Obtain the index of the item at the mouse pointer.
int dragIndex = dragToItem.Index;
ListViewItem[] sel = newListViewItem[listView2.SelectedItems.Count];
for (int i = 0; i <= listView2.SelectedItems.Count - 1; i++)
{
sel[i] = listView2.SelectedItems[i];
}
for (int i = 0; i < sel.GetLength(0); i++)
{
//Obtain the ListViewItem to be dragged to the target location.
ListViewItem dragItem = sel[i];
int itemIndex = dragIndex;
if (itemIndex == dragItem.Index)
{
return;
}
if (dragItem.Index < itemIndex)
itemIndex++;
else
itemIndex = dragIndex + i;
//Insert the item at the mouse pointer.
ListViewItem insertItem = (ListViewItem)dragItem.Clone();
listView2.Items.Insert(itemIndex, insertItem);
//Removes the item from the initial location while
//the item is moved to the new location.
listView2.Items.Remove(dragItem);
}
listView2.View = View.Details;
listView2.View = View.LargeIcon;
}
Last edited by QuinnJohns; November 10th, 2011 at 11:17 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|