VB 怎样实现把一个listbox中的选项鼠标拖动到另一个listbox中
展开全部
Public Class Form1
'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
'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
展开全部
设置list1的OLEDragMode 为1
list2的OLEDropMode 为1
Private Sub List2_OLEDragDrop(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, X As Single, Y As Single)
List2.AddItem (List1.List(List1.ListIndex))
List1.RemoveItem (List1.ListIndex)
End Sub
list2的OLEDropMode 为1
Private Sub List2_OLEDragDrop(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, X As Single, Y As Single)
List2.AddItem (List1.List(List1.ListIndex))
List1.RemoveItem (List1.ListIndex)
End Sub
追问
你说的是在VB6.0的环境下。我想问的是在VB2008的环境下怎么弄?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询