C#listview拖动项目的问题?
以下代码部分实现了拖动,就是有的可以有的不可以,单步调试可以看到,拖动后listView的item项的顺序是按要求改变了,但在窗体上显示的却不是按item的顺序进行排列的...
以下代码部分实现了拖动,就是有的可以有的不可以,单步调试可以看到,拖动后listView的item项的顺序是按要求改变了,但在窗体上显示的却不是按item的顺序进行排列的。
private void listView1_ItemDrag(object sender, ItemDragEventArgs e)
{
listView1.DoDragDrop(listView1.SelectedItems, DragDropEffects.Move);
}
private void listView1_DragEnter(object sender, DragEventArgs e)
{
for (int i = 0; i <= e.Data.GetFormats().Length - 1; i++)
{
if (e.Data.GetFormats()[i].Equals("System.Windows.Forms.ListView+SelectedListViewItemCollection"))
{
e.Effect = DragDropEffects.Move;
}
}
}
private void listView1_DragDrop(object sender, DragEventArgs e)
{
if (listView1.SelectedItems.Count == 0)
{
return;
}
Point cp = listView1.PointToClient(new Point(e.X, e.Y));
ListViewItem dragToItem = listView1.GetItemAt(cp.X,cp.Y);
if (dragToItem == null)
{
return;
}
int dragIndex = dragToItem.Index;
ListViewItem[] sel = new ListViewItem[listView1.SelectedItems.Count];
for (int i = 0; i < listView1.SelectedItems.Count; i++)
{
sel[i] =listView1.SelectedItems[i];
}
for (int i = 0; i < sel.GetLength(0); i++)
{
ListViewItem dragItem = sel[i];
int itemIndex = dragIndex;
if (itemIndex == dragItem.Index)
{
return;
}
if (dragItem.Index < itemIndex)
{
itemIndex++;
}
else
itemIndex = dragIndex + i;
ListViewItem insertItem = (ListViewItem)dragItem.Clone();
listView1.Items.Insert(itemIndex,insertItem);
listView1.Items.Remove(dragItem);
//listView1.Items.Insert(itemIndex, insertItem);
}
} 展开
private void listView1_ItemDrag(object sender, ItemDragEventArgs e)
{
listView1.DoDragDrop(listView1.SelectedItems, DragDropEffects.Move);
}
private void listView1_DragEnter(object sender, DragEventArgs e)
{
for (int i = 0; i <= e.Data.GetFormats().Length - 1; i++)
{
if (e.Data.GetFormats()[i].Equals("System.Windows.Forms.ListView+SelectedListViewItemCollection"))
{
e.Effect = DragDropEffects.Move;
}
}
}
private void listView1_DragDrop(object sender, DragEventArgs e)
{
if (listView1.SelectedItems.Count == 0)
{
return;
}
Point cp = listView1.PointToClient(new Point(e.X, e.Y));
ListViewItem dragToItem = listView1.GetItemAt(cp.X,cp.Y);
if (dragToItem == null)
{
return;
}
int dragIndex = dragToItem.Index;
ListViewItem[] sel = new ListViewItem[listView1.SelectedItems.Count];
for (int i = 0; i < listView1.SelectedItems.Count; i++)
{
sel[i] =listView1.SelectedItems[i];
}
for (int i = 0; i < sel.GetLength(0); i++)
{
ListViewItem dragItem = sel[i];
int itemIndex = dragIndex;
if (itemIndex == dragItem.Index)
{
return;
}
if (dragItem.Index < itemIndex)
{
itemIndex++;
}
else
itemIndex = dragIndex + i;
ListViewItem insertItem = (ListViewItem)dragItem.Clone();
listView1.Items.Insert(itemIndex,insertItem);
listView1.Items.Remove(dragItem);
//listView1.Items.Insert(itemIndex, insertItem);
}
} 展开
1个回答
展开全部
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询