468,272 Members | 2,011 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,272 developers. It's quick & easy.

Listbox DragnDrop

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.T ext).ToString)
lstAvailable.Items.Remove(e.Data.GetData(DataForma ts.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(DataFormat s.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.SelectedIte m)
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

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
Nov 21 '05 #1
3 3050
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.ListVi ewItem()") 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.Ite m(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.Ite m(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

<th*****@msala.net> wrote in message
news:42**********************@news.newsdemon.com.. .
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.T ext).ToString)

lstAvailable.Items.Remove(e.Data.GetData(DataForma ts.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(DataFormat s.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.SelectedIte m)
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

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access

Nov 21 '05 #2
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

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
Nov 21 '05 #3
Here's an article that shows you how to drag/drop with a listbox control:

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

I hope this helps,

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

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

17 posts views Thread by amber | last post: by
3 posts views Thread by Paul T. Rong | last post: by
8 posts views Thread by Oddball | last post: by
6 posts views Thread by Chris Leuty | last post: by
7 posts views Thread by Dave | last post: by
2 posts views Thread by Sam | last post: by
3 posts views Thread by Ali Chambers | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.