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

VB.NET 2005 - Drag & drop between listboxes

P: n/a
In VB.NET 2005 (winform) any sample code to drag & drop items between 2
listboxes? Thanks!
Nov 23 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Including, how can I reposition the item in the listbox by drag/drop?

"VB Programmer" <do**@emailme.com> wrote in message
news:OK**************@TK2MSFTNGP15.phx.gbl...
In VB.NET 2005 (winform) any sample code to drag & drop items between 2
listboxes? Thanks!

Nov 23 '05 #2

P: n/a
Hi,

I see that no one has answered this yet. I have done some work in this area
but I don't have the time to respond right now. I will respond this
evening, however. Sorry for the delay.

If you don't hear from me by this evening, please email me at
be*****@optonline.net.

Bernie Yaeger

"VB Programmer" <do**@emailme.com> wrote in message
news:OK**************@TK2MSFTNGP15.phx.gbl...
In VB.NET 2005 (winform) any sample code to drag & drop items between 2
listboxes? Thanks!

Nov 23 '05 #3

P: n/a
Hi,

I've done some work on listbox (listview) drag/drop, but I have to
acknowledge that I have no way of reordering the items as they pass into the
'drop' box. However, I do know a little about this, so here's my answer.

I note that you mention vb .net 2005 specifically: I am not aware of a
difference between it and vb .net 2003 regarding drag/drop, but I apologize
if there are differences and it would be worth looking into.

BTW - MS is almost empty on the subject of drag/drop - a modest intro here
and there, but that's about it (and some of those are incorrect).

In any event, you have to modify the mousedown, dragdrop, and dragenter
events. I have code samples below. However, I have found that I have more
control using listviews instead of listboxes. For the listview to work, you
have to set the listview's view to smallicon mode; the rest is similar but
slightly different (the itemdrag event has also to be altered). I have
actually created a dual listview drag/drop control. If you would like to
see it, just email me at be*****@optonline.net, tell me your email address,
and I will send it to you.

HTH,

Bernie Yaeger

Private Sub frombox3_MouseDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles frombox3.MouseDown

Dim Pt As New Point(e.X, e.Y)

Dim Index As Integer

' Determines which item was selected.

frombox3 = sender

Index = frombox3.IndexFromPoint(Pt)

' Starts a drag-and-drop operation with that item.

If Index >= 0 Then

frombox3.DoDragDrop(frombox3.Items(Index), DragDropEffects.Move)

End If

End Sub

Private Sub tobox3_MouseDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles tobox3.MouseDown

Dim Pt As New Point(e.X, e.Y)

Dim Index As Integer

' Determines which item was selected.

tobox3 = sender

Index = tobox3.IndexFromPoint(Pt)

' Starts a drag-and-drop operation with that item.

If Index >= 0 Then

tobox3.DoDragDrop(tobox3.Items(Index), DragDropEffects.Move)

End If

End Sub

Private Sub frombox3_DragDrop(ByVal sender As Object, ByVal e As
System.Windows.Forms.DragEventArgs) Handles frombox3.DragDrop

frombox3.Items.Add(e.Data.GetData(DataFormats.Text ).ToString)

tobox3.Items.Remove(e.Data.GetData(DataFormats.Tex t).ToString)

End Sub

Private Sub tobox3_DragDrop(ByVal sender As Object, ByVal e As
System.Windows.Forms.DragEventArgs) Handles tobox3.DragDrop

tobox3.Items.Add(e.Data.GetData(DataFormats.Text). ToString)

frombox3.Items.Remove(e.Data.GetData(DataFormats.T ext).ToString)

End Sub

Private Sub tobox3_DragEnter(ByVal sender As Object, ByVal e As
System.Windows.Forms.DragEventArgs) Handles tobox3.DragEnter

Dim x As Integer

x =
tobox3.FindStringExact(e.Data.GetData(DataFormats. Text).ToString, -1)

If x <> -1 Then

Exit Sub

End If

If (e.Data.GetDataPresent(DataFormats.Text)) Then

e.Effect = DragDropEffects.Move

Else

e.Effect = DragDropEffects.None

End If

End Sub

Private Sub frombox3_DragEnter(ByVal sender As Object, ByVal e As
System.Windows.Forms.DragEventArgs) Handles frombox3.DragEnter

Dim x As Integer

x =
frombox3.FindStringExact(e.Data.GetData(DataFormat s.Text).ToString, -1)

If x <> -1 Then

Exit Sub

End If

If (e.Data.GetDataPresent(DataFormats.Text)) Then

e.Effect = DragDropEffects.Move

Else

e.Effect = DragDropEffects.None

End If

End Sub

"VB Programmer" <do**@emailme.com> wrote in message
news:OK**************@TK2MSFTNGP15.phx.gbl...
In VB.NET 2005 (winform) any sample code to drag & drop items between 2
listboxes? Thanks!

Nov 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.