By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,650 Members | 1,823 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,650 IT Pros & Developers. It's quick & easy.

Reordering listbox items with drag and drop

P: n/a
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
Nov 21 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Hi John,

Here's the solution:

http://support.microsoft.com/default...b;en-us;306969

I hope this helps

Crouchie1998
BA (HONS) MCP MCSE
Nov 21 '05 #2

P: n/a
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
On Fri, 17 Jun 2005 22:57:05 +0100, "Crouchie1998"
<cr**********@spamcop.net> wrote:
Hi John,

Here's the solution:

http://support.microsoft.com/default...b;en-us;306969

I hope this helps

Crouchie1998
BA (HONS) MCP MCSE


Nov 21 '05 #3

P: n/a
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.ListVi ewItem()")
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

Nov 21 '05 #4

P: n/a
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.

Nov 21 '05 #5

P: n/a
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.

Nov 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.