Drag and Drop within one list box

  • Thread starter Thread starter JL
  • Start date Start date
J

JL

I have found may examples for dragging and dropping between list boxes
but I need to do it within one. Can some one point me to a tutorial or
example. Prefer VB but can use C# too.

TIA,
John
 
Thank you Jorge for the response. I did find that article when I ran
Google. And I understand doing drag/drop between controls but not
within a single ListBox so my user can re-order the items in the list.

John
 
Ok.

I hope that the next sample of code helps you to understand how can you use
the drag & drop feature in VB.NET. :-)


Regards.



Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
ListBox1.Items.Add("ListBox1 - Ele 1")
ListBox1.Items.Add("ListBox1 - Ele 2")
ListBox1.Items.Add("ListBox1 - Ele 3")
ListBox1.Items.Add("ListBox1 - Ele 4")

ListBox2.Items.Add("ListBox2 - Ele 1")
ListBox2.Items.Add("ListBox2 - Ele 2")
ListBox2.Items.Add("ListBox2 - Ele 3")

ListBox2.AllowDrop = True
End Sub



Private Sub ListBox1_MouseDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseDown
If (ListBox1.Items.Count > 0) Then
Dim index As Integer = ListBox1.IndexFromPoint(e.X, e.Y)
Dim strElementOrigin As String = ListBox1.Items(index).ToString()
Dim dde As DragDropEffects = DoDragDrop(strElementOrigin,
DragDropEffects.All)
If dde = DragDropEffects.All Then
ListBox1.Items.RemoveAt(ListBox1.IndexFromPoint(e.X, e.Y))
End If
End If
End Sub



Private Sub ListBox2_DragOver(ByVal sender As Object, ByVal e As
System.Windows.Forms.DragEventArgs) Handles ListBox2.DragOver
e.Effect = DragDropEffects.All
End Sub



Private Sub ListBox2_DragDrop(ByVal sender As Object, ByVal e As
System.Windows.Forms.DragEventArgs) Handles ListBox2.DragDrop
If e.Data.GetDataPresent(DataFormats.StringFormat) Then
Dim strElementDestiny As String =
e.Data.GetData(DataFormats.StringFormat).ToString()
ListBox2.Items.Add(strElementDestiny)
End If
End Sub

--
Jorge Serrano Pérez
Microsoft MVP VB.NET
PortalVB.com
http://www.portalvb.com/
Weblog de Jorge Serrano
http://weblogs.golemproject.com/jorge/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top