'派哪Listbox之间项目拖动圆搜示例,橘羡历左键移动,右键复制
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ListBox2.AllowDrop = True
End Sub
Private Sub ListBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseDown
Dim DragIndex = ListBox1.IndexFromPoint(e.X, e.Y)
If DragIndex <>ListBox.NoMatches Then
ListBox1.SelectedIndex = DragIndex
If e.Button = Windows.Forms.MouseButtons.Left Then
DoDragDrop(ListBox1.Items(DragIndex), DragDropEffects.Move)
ElseIf e.Button = Windows.Forms.MouseButtons.Right Then
DoDragDrop(ListBox1.Items(DragIndex), DragDropEffects.Copy)
End If
End If
End Sub
Private Sub ListBox2_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListBox2.DragEnter
e.Effect = e.AllowedEffect
End Sub
Private Sub ListBox2_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListBox2.DragDrop
Dim item As Object = CType(e.Data.GetData(GetType(System.String)), System.Object)
Dim item2 As Integer = ListBox2.IndexFromPoint(ListBox2.PointToClient(New Point(e.X, e.Y)))
If item2 = -1 Then
ListBox2.Items.Add(item)
Else
ListBox2.Items.Insert(item2, item)
End If
If e.AllowedEffect = DragDropEffects.Move Then
ListBox1.Items.Remove(item)
End If
End Sub
End Class
主要是drag的两个事件
private void listBox1_DragEnter(object sender, DragEventArgs e){
e.Effect = DragDropEffects.All
}
private void listBox1_DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop, false))
{
String[] files = (String[])e.Data.GetData(DataFormats.FileDrop)
foreach (String s in files)
{
(sender as ListBox).Items.Add(s)
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)