源于csdn网友的提问。帮写的一个程序。
ListView拖动自己的item放到自己另外一个item里面
释放操作里面取得两个item的值后,就可以处理更多相关的事物,操作了。例如文件的移动,复制等,数据交换等。
代码:释放操作里面取得两个item的值后,就可以处理更多相关的事物,操作了。例如文件的移动,复制等,数据交换等。
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.ListView1.AllowDrop = True
Me.ListView1.LargeImageList = Me.ImageList1
Dim item As ListViewItem
item = New ListViewItem
item.Name = "1"
item.Text = "first"
item.ImageIndex = 0
item.Tag = 1
Me.ListView1.Items.Add(item)
item = New ListViewItem
item.Name = "2"
item.Text = "second"
item.ImageIndex = 0
item.Tag = 2
Me.ListView1.Items.Add(item)
End Sub
Private Sub ListView1_DragEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListView1.DragEnter
If e.Data.GetDataPresent(GetType(ListViewItem)) Then
e.Effect = DragDropEffects.Move
End If
End Sub
Private Sub ListView1_ItemDrag(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ItemDragEventArgs) Handles ListView1.ItemDrag
If Me.ListView1.SelectedItems.Count > 0 Then
Me.ListView1.DoDragDrop(CType(Me.ListView1.SelectedItems.Item(0), ListViewItem), DragDropEffects.All)
End If
End Sub
Private Sub ListView1_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListView1.DragDrop
If e.Data.GetDataPresent(GetType(ListViewItem)) Then
'Dim hit As ListViewHitTestInfo = ListView1.HitTest(e.X, e.Y)
'Me.Label1.Text = "ok"
'If hit.Item IsNot Nothing Then
' Me.Label1.Text = "你釋放與item:" & hit.Item.Text
'End If
Dim item As ListViewItem
Dim OnItem As ListViewItem
Dim lv As ListView = CType(sender, ListView)
Dim clX As Integer = lv.PointToClient(New Point(e.X, e.Y)).X
Dim clY As Integer = lv.PointToClient(New Point(e.X, e.Y)).Y
If e.Data.GetDataPresent("System.Windows.Forms.ListViewItem", False) Then
'dragging a listview item
item = CType(e.Data.GetData("System.Windows.Forms.ListViewItem"), ListViewItem)
OnItem = CType(sender, ListView).GetItemAt(clX, clY)
'destLv.Items.Insert(OnItem.Index, item.Clone)
'item.Remove()
Me.Label1.Text = String.Format("你拖动 {0} 并释放在 {1} 上", item.Text, OnItem.Text)
End If
End If
End Sub
End Class