VB.NET 2005 - Drag & drop between listboxes

V

VB Programmer

In VB.NET 2005 (winform) any sample code to drag & drop items between 2
listboxes? Thanks!
 
B

Bernie Yaeger

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
(e-mail address removed).

Bernie Yaeger
 
B

Bernie Yaeger

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 (e-mail address removed), 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.Text).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.Text).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(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
 

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