Reordering listbox items with drag and drop

J

John Dann

Can anyone point me to a tutorial on reordering items within a single
listbox using the mouse, ie drag and drop, specifically with vb.net.
Google shows me various half-references eg to related controls, vb6 or
whatever, but nothing that seems to relate specifically to vb.bet and
reordering within a single listbox control.

Thanks
JGD
 
J

John Dann

Thanks but that's only a partial solution in that it doesn't (unless
I've missed something) cover the particular circumstance that I'm
stuck on, ie drap and drop _within_ a single listbox in order to
reorder items that are already listed. (And how best to manage the
reordering of items once a DragDrop event has been generated.)

What I most need to know is whether there's a relatively simple
solution to this or alternatively, if not, then I should maybe cut my
losses and try a different approach to the problem, eg by using
up/down cursor keys. But it would be neatest to use drag/drop.

JGD
 
S

Shane Stewart

I pulled this code from one of my personal projects. This should do
what you want. It looks like the second subroutine does nothing, but I
may have needed it for some reason, or it may have been for an idea
that I was working on that I never got back to.

At any rate, watch the word wrapping.
Private Sub lvw_DragDrop(ByVal sender As Object, ByVal e As
System.Windows.Forms.DragEventArgs) Handles lvw.DragDrop
Try
Dim i As Integer
Dim l_Point As Point = lvw.PointToClient(New Point(e.X,
e.Y))
Dim l_Item As ListViewItem = lvw.GetItemAt(l_Point.X,
l_Point.Y)
Dim l_Items() As ListViewItem =
e.Data.GetData("System.Windows.Forms.ListViewItem()")
Dim l_DropIndex As Integer = l_Item.Index

btnDelete_Click(sender, New System.EventArgs)
For Each l_Item In l_Items
lvw.Items.Insert(l_DropIndex + i, l_Items(i))
i = i + 1
Next
Catch ex As Exception
End Try
End Sub

Private Sub lvw_DragEnter(ByVal sender As Object, ByVal e As
System.Windows.Forms.DragEventArgs) Handles lvw.DragEnter
If e.Data.GetDataPresent("System.Windows.Forms.ListViewItem()")
Then
e.Effect = DragDropEffects.Move
Else
e.Effect = DragDropEffects.None
End If
End Sub

Private Sub lvw_ItemDrag(ByVal sender As Object, ByVal e As
System.Windows.Forms.ItemDragEventArgs) Handles lvw.ItemDrag
Dim myItem As ListViewItem
Dim myItems(sender.SelectedItems.Count - 1) As ListViewItem
Dim i As Integer = 0

' Loop though the SelectedItems collection for the source.
For Each myItem In sender.SelectedItems
' Add the ListViewItem to the array of ListViewItems.
myItems(i) = myItem
i = i + 1
Next
' Create a DataObject containg the array of ListViewItems.
sender.DoDragDrop(New
DataObject("System.Windows.Forms.ListViewItem()", myItems),
DragDropEffects.Move)
End Sub
 
S

Shane Stewart

Sorry, I didn't notice at first that you stated a listbox instead of a
listview. I don't use listboxes, so I don't know if that my above code
will work for you.
 
S

Shane Stewart

Sorry, I didn't notice at first that you stated a listbox instead of a
listview. I don't use listboxes, so I don't know if that my above code
will work for you.
 

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

Top