c# Winform Listview的Details模式时拖动Items排序的实现。
时间:2011-06-15 来源:四喜
本部分代码参考与网络文章,部分代码转自网络,感谢提供思路的作者。
/// <summary>
/// 当拖动某项时
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void listView1_ItemDrag(object sender, ItemDragEventArgs e)
{
listView1.DoDragDrop(e.Item, DragDropEffects.Move);
}
/// <summary>
/// 用鼠标拖动某项至该控件的区域时
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void listView1_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
/// <summary>
/// 拖动时拖着某项置于某行上方时
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void listView1_DragOver(object sender, DragEventArgs e)
{
Point ptScreen = new Point(e.X, e.Y);
Point pt = listView1.PointToClient(ptScreen);
ListViewItem item = listView1.GetItemAt(pt.X, pt.Y);
if (item != null)
item.Selected = true;
}
/// <summary>
/// 拖动结束
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void listView1_DragDrop(object sender, DragEventArgs e)
{
ListViewItem draggedItem = (ListViewItem)e.Data.GetData(typeof(ListViewItem));
Point ptScreen = new Point(e.X, e.Y);
Point pt = listView1.PointToClient(ptScreen);
ListViewItem TargetItem = listView1.GetItemAt(pt.X, pt.Y);//拖动的项将放置于该项之前
listView1.Items.Insert(TargetItem.Index, (ListViewItem)draggedItem.Clone());
listView1.Items.Remove(draggedItem);
}
相关阅读 更多 +