以下是该方案:
总而言之,使用鼠标左键或右键从Listview拖动的项目的工作方式相同。但是,一旦放置目标从Listview中删除了一个项目(即使它是拖动的项目也不重要),鼠标右键拖动将停止工作,直到使用鼠标左键单击Listview项目。
这是预期的行为,如果是这样,为什么?有解决方法,所以我可以让鼠标左键和右键同时工作吗?
以下是相关代码:
Private Sub ListView1_ItemDrag(sender As Object, e As ItemDragEventArgs) Handles ListView1.ItemDrag
ListView1.DoDragDrop(ListView1.SelectedItems.Item(0), DragDropEffects.Link)
End Sub
Private Sub TextBox1_DragDrop(sender As Object, e As DragEventArgs) Handles TextBox1.DragDrop
Dim item As ListViewItem = e.Data.GetData(GetType(ListViewItem))
If rbRemove.Checked Then
Dim index As Int32 = ListView1.SelectedIndices.Item(0)
For i As Int32 = 0 To ListView1.Items.Count - 1
Dim item1 As ListViewItem = ListView1.Items(i)
If item1 Is item Then
ListView1.Items.RemoveAt(i)
Exit For
End If
If index >= ListView1.Items.Count Then
index -= 1
End If
ListView1.Items(index).Selected = True
Next
End If
End Sub
Private Sub TextBox1_DragOver(sender As Object, e As DragEventArgs) Handles TextBox1.DragOver
e.Effect = DragDropEffects.Link
End Sub
答案 0 :(得分:0)
我不想这样做,但这似乎是一种解决方法:
Private activeDrag As Boolean
Private Sub ListView1_MouseMove(sender As Object, e As MouseEventArgs) Handles ListView1.MouseMove
If (Not activeDrag) AndAlso (e.Button = Windows.Forms.MouseButtons.Right) AndAlso (Me.ListView1.SelectedItems.Count > 0) Then
Dim x As ItemDragEventArgs = New ItemDragEventArgs(e.Button, Me.ListView1.SelectedItems.Item(0))
Me.ListView1_ItemDrag(Me, x)
End If
End Sub
在调用DoDragDrop之前,activeDrag变量设置为true,然后在返回时设置为false。