DragnDrop Listview Items

T

thomasp

Has anyone got some sample code to do drag and drop from one listbox to
another listbox using VB.Net 2005. The below code works for draging and
droping one at a time, but not for multiselected items. I tried setting up
an array to capture the selected items and then move them with the dragndrop
code, but after selecting the items when the user clicks on the items to
drag them the selection goes back to one item. Also I have code for the
listbox doubleclick event that moves whatever item is doubleclicked in one
listbox to the other. This worked fine until I added the dragndrop code to
the mousedown event, now instead of a doubleclick event I get two mousedown
events. I am sure someone has done this and I would appreciate seeing the
code.

Thanks

Thomas

Private Sub lstSelected_DragDrop(ByVal sender As Object, ByVal e As
System.Windows.Forms.DragEventArgs) Handles lstSelected.DragDrop
lstSelected.Items.Add(e.Data.GetData(DataFormats.Text).ToString)
lstAvailable.Items.Remove(e.Data.GetData(DataFormats.Text).ToString)
End Sub

Private Sub lstAvailable_DragDrop(ByVal sender As Object, ByVal e As
System.Windows.Forms.DragEventArgs) Handles lstAvailable.DragDrop
lstAvailable.Items.Add(e.Data.GetData(DataFormats.Text).ToString)
lstSelected.Items.Remove(e.Data.GetData(DataFormats.Text).ToString)
End Sub

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

If (e.Data.GetDataPresent(DataFormats.Text)) Then
e.Effect = DragDropEffects.Move
Else
e.Effect = DragDropEffects.None
End If

End Sub

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

If (e.Data.GetDataPresent(DataFormats.Text)) Then
e.Effect = DragDropEffects.Move
Else
e.Effect = DragDropEffects.None
End If

End Sub

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

strDragDrop = lstAvailable.Text
lstAvailable.DoDragDrop(strDragDrop, DragDropEffects.Move)
End Sub

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

strDragDrop = lstSelected.Text
lstSelected.DoDragDrop(strDragDrop, DragDropEffects.Move)
End Sub


Private Sub lstAvailable_MouseDoubleClick(ByVal sender As Object, ByVal e As
System.EventArgs) Handles lstAvailable.DoubleClick
lstSelected.Items.Add(lstAvailable.SelectedItem)
lstAvailable.Items.Remove(lstAvailable.SelectedItem)
End Sub

Private Sub lstSelected_DoubleClick(ByVal sender As Object, ByVal e As
System.EventArgs) Handles lstSelected.DoubleClick
lstAvailable.Items.Add(lstSelected.SelectedItem)
lstSelected.Items.Remove(lstSelected.SelectedItem)
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

Similar Threads


Top