注意看Listview的事件
[C#] 纯文本查看 复制代码 /// <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);
}
|