列表框拖放项目以移动到同一列表框中的不同位置 [英] Listbox drag and drop items to move to different locations in the same listbox

查看:82
本文介绍了列表框拖放项目以移动到同一列表框中的不同位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚如何使用拖放事件来切换列表框项目的顺序。每当我尝试使用我的代码时,它会不断给我一个圆圈,并在其中划一条线。无论我做什么,我都无法让他们移动。



I am trying to figure out how to use the drag and drop event to switch the order of the listbox items. every time I try to use my code it keeps giving me a circle with a line through it. no matter what I do I cant get them to move.

Private Sub ListBox1_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseDown

       Dim index As Integer = ListBox1.IndexFromPoint(e.X, e.Y)
       Dim s As String = ListBox1.Items(index).ToString()
       Dim dde1 As DragDropEffects = DoDragDrop(s, DragDropEffects.Move)

       If dde1 = DragDropEffects.All Then
           ListBox1.Items.RemoveAt(ListBox1.IndexFromPoint(e.X, e.Y))
       End If


   End Sub
   Private Sub ListBox1_DragOver(sender As Object, e As System.Windows.Forms.DragEventArgs) Handles ListBox1.DragEnter

       e.Effect = DragDropEffects.All
   End Sub

   Private Sub ListBox1_DragDrop(sender As Object, e As System.Windows.Forms.DragEventArgs) Handles ListBox1.DragDrop


       If e.Data.GetDataPresent(DataFormats.Text) Then
           e.Effect = DragDropEffects.Move
           ListBox1.SelectedIndex = ListBox1.IndexFromPoint(ListBox1.PointToClient(New Point(e.X, e.Y)))
       End If
       Dim str As String = DirectCast(e.Data.GetData(DataFormats.StringFormat), String)
       ListBox1.Items.Insert(ListBox1.SelectedIndex, str)


   End Sub

< br $> b $ b

我尝试了什么:



已更改。移至.copy但是我仍然通过它获得相同的圆圈。



What I have tried:

changed .move to .copy but I still get the same circle with the line through it.

推荐答案

我建​​议阅读:在Visual Basic .NET中实现拖放 [ ^ ]



至于你的代码...

ListBox1_DragDrop 程序中你必须添加这一行:

I'd suggest to read this: Implementing Drag and Drop in Visual Basic .NET[^]

As to your code...
Inside ListBox1_DragDrop procedure you have to add this line:
Dim myitem = DataFormats.Text
ListBox1.Items.Add(mtitem)



上面的代码可让你复制项目。现在,您必须提供代码来移除已移动的项目。


Above code enable you to copy item. Now, you have to provide code to remove item, which have ben moved.


这篇关于列表框拖放项目以移动到同一列表框中的不同位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆