Listbox DragnDrop

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
 
B

Bernie Yaeger

Hi Thomas,

It's a real problem. I'd been trying to get this figured out for some time,
and I finally turned to a kludge: listviews in smallicon view, which is a
listbox for all intents and purposes. Then I created a control that places
2 such listviews on a form with the drag/drop capabilities built in.

The basic code for this is below, but if you like, I can send you the
solution so you can see all the code and, once compiled, you can add the
double listviews to your toolbox.

HTH,

Bernie Yaeger
Imports System.ComponentModel

Public Class listviewdragdrop

Inherits System.Windows.Forms.UserControl

Enum boxchoice As Integer

left

right

End Enum

#Region " Windows Form Designer generated code "

Public Sub New()

MyBase.New()

'This call is required by the Windows Form Designer.

InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'UserControl1 overrides dispose to clean up the component list.

Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)

If disposing Then

If Not (components Is Nothing) Then

components.Dispose()

End If

End If

MyBase.Dispose(disposing)

End Sub

'Required by the Windows Form Designer

Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer

'It can be modified using the Windows Form Designer.

'Do not modify it using the code editor.

Friend WithEvents ListView1 As System.Windows.Forms.ListView

Friend WithEvents ListView2 As System.Windows.Forms.ListView

<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

Me.ListView1 = New System.Windows.Forms.ListView

Me.ListView2 = New System.Windows.Forms.ListView

Me.SuspendLayout()

'

'ListView1

'

Me.ListView1.AllowDrop = True

Me.ListView1.Location = New System.Drawing.Point(8, 8)

Me.ListView1.Name = "ListView1"

Me.ListView1.Size = New System.Drawing.Size(121, 120)

Me.ListView1.TabIndex = 0

Me.ListView1.TabStop = False

Me.ListView1.View = System.Windows.Forms.View.SmallIcon

'

'ListView2

'

Me.ListView2.AllowDrop = True

Me.ListView2.Location = New System.Drawing.Point(144, 8)

Me.ListView2.Name = "ListView2"

Me.ListView2.Size = New System.Drawing.Size(121, 120)

Me.ListView2.TabIndex = 1

Me.ListView2.TabStop = False

Me.ListView2.View = System.Windows.Forms.View.SmallIcon

'

'listviewdragdrop

'

Me.AllowDrop = True

Me.Controls.Add(Me.ListView2)

Me.Controls.Add(Me.ListView1)

Me.Name = "listviewdragdrop"

Me.Size = New System.Drawing.Size(272, 136)

Me.ResumeLayout(False)

End Sub

#End Region

Function selecteditem(ByVal x As boxchoice, ByVal i As Integer) As String

If x = boxchoice.left Then

selecteditem = ListView1.Items(i).Text

Else

selecteditem = ListView2.Items(i).Text

End If

'selecteditem = ListView2.Items(i).Text

End Function

Function countselected(ByVal x As boxchoice) As Integer

If x = boxchoice.left Then

countselected = ListView1.Items.Count

Else

countselected = ListView2.Items.Count

End If

End Function

Function addleft(ByVal t As String) As ListViewItem

addleft = ListView1.Items.Add(t)

End Function

Function addright(ByVal t As String) As ListViewItem

addright = ListView2.Items.Add(t)

End Function

Private Sub ListView_ItemDrag(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.ItemDragEventArgs) Handles ListView1.ItemDrag,
ListView2.ItemDrag

Dim myitem As ListViewItem

Dim myitems(sender.SelectedItems.Count - 1) As ListViewItem

Dim i As Integer = 0

For Each myitem In sender.SelectedItems

myitems(i) = myitem

i = i + 1

Next

sender.allowdrop = False

sender.dodragdrop(New DataObject("System.Windows.Forms.ListViewItem()",
myitems), DragDropEffects.Move)

End Sub

Private Sub ListView_DragEnter(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.DragEventArgs) Handles ListView1.DragEnter,
ListView2.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 ListView1_DragDrop(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.DragEventArgs) Handles ListView1.DragDrop

Dim myitem As ListViewItem

Dim myitems() As ListViewItem =
e.Data.GetData("System.Windows.Forms.ListViewItem()")

Dim i As Integer = 0

For Each myitem In myitems

sender.Items.Add(myitems(i).Text)

ListView2.Items.Remove(ListView2.SelectedItems.Item(0))

i = i + 1

Next

ListView1.AllowDrop = True

ListView2.AllowDrop = True

End Sub

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

Dim myitem As ListViewItem

Dim myitems() As ListViewItem =
e.Data.GetData("System.Windows.Forms.ListViewItem()")

Dim i As Integer = 0

For Each myitem In myitems

sender.Items.Add(myitems(i).Text)

ListView1.Items.Remove(ListView1.SelectedItems.Item(0))

i = i + 1

Next

ListView1.AllowDrop = True

ListView2.AllowDrop = True

End Sub



Private Sub ListView_MouseDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseDown,
ListView2.MouseDown

ListView1.AllowDrop = True

ListView2.AllowDrop = True

End Sub

Private Sub ListView_MouseUp(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseUp,
ListView2.MouseUp

ListView1.AllowDrop = True

ListView2.AllowDrop = True

End Sub

End Class
 
T

thomasp

I used the listview and the following code. Seems to work fine.

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

'Retrieve the data in the string array format.
Dim myText() As String = e.Data.GetData("System.String[]")
Dim i As Integer
For i = 0 To myText.Length - 1
'Add the dragged items to the ListView control.
lstAvailable.Items.Add(myText(i))
Next

End Sub

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

'Check for the DataFormat string array.
If e.Data.GetDataPresent("System.String[]") Then
'If the data stored is a string array,
'set the Effect of drag-and-drop operation to Move.
e.Effect = DragDropEffects.Move
Else
'Else set the Effect of drag-and-drop operation to None.
e.Effect = DragDropEffects.None
End If

End Sub

Private Sub lstAvailable_ItemDrag(ByVal sender As Object, ByVal e As
System.Windows.Forms.ItemDragEventArgs) Handles lstAvailable.ItemDrag

Dim myItem As ListViewItem
'Create an array of strings.
Dim myItems(lstAvailable.SelectedItems.Count - 1) As String
Dim i As Integer = 0
'Loop though the SelectedItems collection of the ListView control.
For Each myItem In lstAvailable.SelectedItems
'Add the Text of the ListViewItem to the array.
myItems(i) = myItem.Text
i = i + 1
Next
'DoDragDrop begins the drag-and-drop operation.
'The data to be dragged is the array of strings.
lstAvailable.DoDragDrop(myItems, DragDropEffects.Move)
Dim j As ListViewItem
For Each j In lstAvailable.SelectedItems
'Remove the ListViewItem from the ListView control.
lstAvailable.Items.Remove(j)
Next

End Sub

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

'Retrieve the data in the string array format.
Dim myText() As String = e.Data.GetData("System.String[]")
Dim i As Integer
For i = 0 To myText.Length - 1
'Add the dragged items to the ListView control.
lstSelected.Items.Add(myText(i))
Next

End Sub

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

'Check for the DataFormat string array.
If e.Data.GetDataPresent("System.String[]") Then
'If the data stored is a string array,
'set the Effect of drag-and-drop operation to Move.
e.Effect = DragDropEffects.Move
Else
'Else set the Effect of drag-and-drop operation to None.
e.Effect = DragDropEffects.None
End If

End Sub

Private Sub lstSelected_ItemDrag(ByVal sender As Object, ByVal e As
System.Windows.Forms.ItemDragEventArgs) Handles lstSelected.ItemDrag

Dim myItem As ListViewItem
'Create an array of strings.
Dim myItems(lstSelected.SelectedItems.Count - 1) As String
Dim i As Integer = 0
'Loop though the SelectedItems collection of the ListView control.
For Each myItem In lstSelected.SelectedItems
'Add the Text of the ListViewItem to the array.
myItems(i) = myItem.Text
i = i + 1
Next
'DoDragDrop begins the drag-and-drop operation.
'The data to be dragged is the array of strings.
lstSelected.DoDragDrop(myItems, DragDropEffects.Move)
Dim j As ListViewItem
For Each j In lstSelected.SelectedItems
'Remove the ListViewItem from the ListView control.
lstSelected.Items.Remove(j)
Next

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