473,386 Members | 1,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 3273
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Anjali | last post by:
Hi, I want to implement DragnDrop across th forms of the application. I am able to get some examples to implement DragnDrop across the controls in the same form but I have to implement that...
17
by: amber | last post by:
Hello. Can someone tell me what I may be doing wrong here? I'm using the code (lboxRP is a listbox): Dim newRPindex As Integer newRPindex = Me.lboxRP.FindString(RP)...
3
by: Paul T. Rong | last post by:
I have a listbox (of product names) control on my form. I want to pass the selected item (a product name) to a subform, and the product unitprice should apear automatically next to the product name...
8
by: Oddball | last post by:
Ok - I have a ListBox control and I'm ready to write my own DrawItem event handler. What I want to draw as the item is another control. I have created a user control that I would like to list in...
6
by: Chris Leuty | last post by:
I am populating a multiselect Listbox from a dataset, with the content of the listbox filled by one table, and the selections determined from another table. So far, I have been keeping the dataset...
7
by: Dave | last post by:
Hi all, After unsuccessfully trying to make my own dual listbox control out of arraylists, I decided to look for a 3rd party control. I've looked for over a week now and can't find anything but...
2
by: Sam | last post by:
Hi, In my form I have two ToolStripPanels (TSP), one docked on the left, the other docked on the right. Each of those TSP contains a ToolSrip control. I would like to allow the ToolStrips to...
3
by: Ali Chambers | last post by:
Hi, I have created a listbox called "dtlist1" on my VB.NET form. I call a procedure as follows: Private Sub openfile(flname As String) dtlist1.Items.Clear() etc..
1
by: Sunray | last post by:
I have a form called the sales form and i have 2 sets of listboxes So what happens is. i add items form the bottom set of list boxes which are bound to a data base to the top set of list boxes which...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.