如何把ListBox中的内容拖曳到另一个ListBox中

如何把ListBox中的内容拖曳到另一个ListBox中,第1张

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

主要是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)

                }

            }

        }


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/8206986.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-14
下一篇 2023-04-14

发表评论

登录后才能评论

评论列表(0条)

保存