473,668 Members | 2,348 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_Dra gDrop(ByVal sender As Object, ByVal e As
System.Windows. Forms.DragEvent Args) Handles lstSelected.Dra gDrop
lstSelected.Ite ms.Add(e.Data.G etData(DataForm ats.Text).ToStr ing)
lstAvailable.It ems.Remove(e.Da ta.GetData(Data Formats.Text).T oString)
End Sub

Private Sub lstAvailable_Dr agDrop(ByVal sender As Object, ByVal e As
System.Windows. Forms.DragEvent Args) Handles lstAvailable.Dr agDrop
lstAvailable.It ems.Add(e.Data. GetData(DataFor mats.Text).ToSt ring)
lstSelected.Ite ms.Remove(e.Dat a.GetData(DataF ormats.Text).To String)
End Sub

Private Sub lstSelected_Dra gEnter(ByVal sender As Object, ByVal e As
System.Windows. Forms.DragEvent Args) Handles lstSelected.Dra gEnter

If (e.Data.GetData Present(DataFor mats.Text)) Then
e.Effect = DragDropEffects .Move
Else
e.Effect = DragDropEffects .None
End If

End Sub

Private Sub lstAvailable_Dr agEnter(ByVal sender As Object, ByVal e As
System.Windows. Forms.DragEvent Args) Handles lstAvailable.Dr agEnter

If (e.Data.GetData Present(DataFor mats.Text)) Then
e.Effect = DragDropEffects .Move
Else
e.Effect = DragDropEffects .None
End If

End Sub

Private Sub lstAvailable_Mo useDown(ByVal sender As Object, ByVal e As
System.Windows. Forms.MouseEven tArgs) Handles lstAvailable.Mo useDown

strDragDrop = lstAvailable.Te xt
lstAvailable.Do DragDrop(strDra gDrop, DragDropEffects .Move)
End Sub

Private Sub lstSelected_Mou seDown(ByVal sender As Object, ByVal e As
System.Windows. Forms.MouseEven tArgs) Handles lstSelected.Mou seDown

strDragDrop = lstSelected.Tex t
lstSelected.DoD ragDrop(strDrag Drop, DragDropEffects .Move)
End Sub
Private Sub lstAvailable_Mo useDoubleClick( ByVal sender As Object, ByVal e As
System.EventArg s) Handles lstAvailable.Do ubleClick
lstSelected.Ite ms.Add(lstAvail able.SelectedIt em)
lstAvailable.It ems.Remove(lstA vailable.Select edItem)
End Sub

Private Sub lstSelected_Dou bleClick(ByVal sender As Object, ByVal e As
System.EventArg s) Handles lstSelected.Dou bleClick
lstAvailable.It ems.Add(lstSele cted.SelectedIt em)
lstSelected.Ite ms.Remove(lstSe lected.Selected Item)
End Sub

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.c om<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
Nov 21 '05 #1
3 3297
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.Componen tModel

Public Class listviewdragdro p

Inherits System.Windows. Forms.UserContr ol

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.

InitializeCompo nent()

'Add any initialization after the InitializeCompo nent() 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.Disp ose()

End If

End If

MyBase.Dispose( disposing)

End Sub

'Required by the Windows Form Designer

Private components As System.Componen tModel.IContain er

'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.Diagnos tics.DebuggerSt epThrough()> Private Sub InitializeCompo nent()

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

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

Me.SuspendLayou t()

'

'ListView1

'

Me.ListView1.Al lowDrop = True

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

Me.ListView1.Na me = "ListView1"

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

Me.ListView1.Ta bIndex = 0

Me.ListView1.Ta bStop = False

Me.ListView1.Vi ew = System.Windows. Forms.View.Smal lIcon

'

'ListView2

'

Me.ListView2.Al lowDrop = True

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

Me.ListView2.Na me = "ListView2"

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

Me.ListView2.Ta bIndex = 1

Me.ListView2.Ta bStop = False

Me.ListView2.Vi ew = System.Windows. Forms.View.Smal lIcon

'

'listviewdragdr op

'

Me.AllowDrop = True

Me.Controls.Add (Me.ListView2)

Me.Controls.Add (Me.ListView1)

Me.Name = "listviewdragdr op"

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

Me.ResumeLayout (False)

End Sub

#End Region

Function selecteditem(By Val 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(B yVal 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_ItemDr ag(ByVal sender As System.Object, ByVal e As
System.Windows. Forms.ItemDragE ventArgs) Handles ListView1.ItemD rag,
ListView2.ItemD rag

Dim myitem As ListViewItem

Dim myitems(sender. SelectedItems.C ount - 1) As ListViewItem

Dim i As Integer = 0

For Each myitem In sender.Selected Items

myitems(i) = myitem

i = i + 1

Next

sender.allowdro p = False

sender.dodragdr op(New DataObject("Sys tem.Windows.For ms.ListViewItem ()",
myitems), DragDropEffects .Move)

End Sub

Private Sub ListView_DragEn ter(ByVal sender As System.Object, ByVal e As
System.Windows. Forms.DragEvent Args) Handles ListView1.DragE nter,
ListView2.DragE nter

If e.Data.GetDataP resent("System. Windows.Forms.L istViewItem()") Then

e.Effect = DragDropEffects .Move

Else

e.Effect = DragDropEffects .None

End If

End Sub

Private Sub ListView1_DragD rop(ByVal sender As System.Object, ByVal e As
System.Windows. Forms.DragEvent Args) Handles ListView1.DragD rop

Dim myitem As ListViewItem

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

Dim i As Integer = 0

For Each myitem In myitems

sender.Items.Ad d(myitems(i).Te xt)

ListView2.Items .Remove(ListVie w2.SelectedItem s.Item(0))

i = i + 1

Next

ListView1.Allow Drop = True

ListView2.Allow Drop = True

End Sub

Private Sub ListView2_DragD rop(ByVal sender As System.Object, ByVal e As
System.Windows. Forms.DragEvent Args) Handles ListView2.DragD rop

Dim myitem As ListViewItem

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

Dim i As Integer = 0

For Each myitem In myitems

sender.Items.Ad d(myitems(i).Te xt)

ListView1.Items .Remove(ListVie w1.SelectedItem s.Item(0))

i = i + 1

Next

ListView1.Allow Drop = True

ListView2.Allow Drop = True

End Sub

Private Sub ListView_MouseD own(ByVal sender As Object, ByVal e As
System.Windows. Forms.MouseEven tArgs) Handles ListView1.Mouse Down,
ListView2.Mouse Down

ListView1.Allow Drop = True

ListView2.Allow Drop = True

End Sub

Private Sub ListView_MouseU p(ByVal sender As Object, ByVal e As
System.Windows. Forms.MouseEven tArgs) Handles ListView1.Mouse Up,
ListView2.Mouse Up

ListView1.Allow Drop = True

ListView2.Allow Drop = 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_Dra gDrop(ByVal sender As Object, ByVal e As
System.Windows. Forms.DragEvent Args) Handles lstSelected.Dra gDrop
lstSelected.Ite ms.Add(e.Data.G etData(DataForm ats.Text).ToStr ing)

lstAvailable.It ems.Remove(e.Da ta.GetData(Data Formats.Text).T oString)
End Sub

Private Sub lstAvailable_Dr agDrop(ByVal sender As Object, ByVal e As
System.Windows. Forms.DragEvent Args) Handles lstAvailable.Dr agDrop
lstAvailable.It ems.Add(e.Data. GetData(DataFor mats.Text).ToSt ring)
lstSelected.Ite ms.Remove(e.Dat a.GetData(DataF ormats.Text).To String)
End Sub

Private Sub lstSelected_Dra gEnter(ByVal sender As Object, ByVal e As
System.Windows. Forms.DragEvent Args) Handles lstSelected.Dra gEnter

If (e.Data.GetData Present(DataFor mats.Text)) Then
e.Effect = DragDropEffects .Move
Else
e.Effect = DragDropEffects .None
End If

End Sub

Private Sub lstAvailable_Dr agEnter(ByVal sender As Object, ByVal e As
System.Windows. Forms.DragEvent Args) Handles lstAvailable.Dr agEnter

If (e.Data.GetData Present(DataFor mats.Text)) Then
e.Effect = DragDropEffects .Move
Else
e.Effect = DragDropEffects .None
End If

End Sub

Private Sub lstAvailable_Mo useDown(ByVal sender As Object, ByVal e As
System.Windows. Forms.MouseEven tArgs) Handles lstAvailable.Mo useDown

strDragDrop = lstAvailable.Te xt
lstAvailable.Do DragDrop(strDra gDrop, DragDropEffects .Move)
End Sub

Private Sub lstSelected_Mou seDown(ByVal sender As Object, ByVal e As
System.Windows. Forms.MouseEven tArgs) Handles lstSelected.Mou seDown

strDragDrop = lstSelected.Tex t
lstSelected.DoD ragDrop(strDrag Drop, DragDropEffects .Move)
End Sub
Private Sub lstAvailable_Mo useDoubleClick( ByVal sender As Object, ByVal e
As
System.EventArg s) Handles lstAvailable.Do ubleClick
lstSelected.Ite ms.Add(lstAvail able.SelectedIt em)
lstAvailable.It ems.Remove(lstA vailable.Select edItem)
End Sub

Private Sub lstSelected_Dou bleClick(ByVal sender As Object, ByVal e As
System.EventArg s) Handles lstSelected.Dou bleClick
lstAvailable.It ems.Add(lstSele cted.SelectedIt em)
lstSelected.Ite ms.Remove(lstSe lected.Selected Item)
End Sub

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.c om<<<<<<------
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_Dr agDrop(ByVal sender As Object, ByVal e As
System.Windows. Forms.DragEvent Args) Handles lstAvailable.Dr agDrop

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

End Sub

Private Sub lstAvailable_Dr agEnter(ByVal sender As Object, ByVal e As
System.Windows. Forms.DragEvent Args) Handles lstAvailable.Dr agEnter

'Check for the DataFormat string array.
If e.Data.GetDataP resent("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_It emDrag(ByVal sender As Object, ByVal e As
System.Windows. Forms.ItemDragE ventArgs) Handles lstAvailable.It emDrag

Dim myItem As ListViewItem
'Create an array of strings.
Dim myItems(lstAvai lable.SelectedI tems.Count - 1) As String
Dim i As Integer = 0
'Loop though the SelectedItems collection of the ListView control.
For Each myItem In lstAvailable.Se lectedItems
'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.Do DragDrop(myItem s, DragDropEffects .Move)
Dim j As ListViewItem
For Each j In lstAvailable.Se lectedItems
'Remove the ListViewItem from the ListView control.
lstAvailable.It ems.Remove(j)
Next

End Sub

Private Sub lstSelected_Dra gDrop(ByVal sender As Object, ByVal e As
System.Windows. Forms.DragEvent Args) Handles lstSelected.Dra gDrop

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

End Sub

Private Sub lstSelected_Dra gEnter(ByVal sender As Object, ByVal e As
System.Windows. Forms.DragEvent Args) Handles lstSelected.Dra gEnter

'Check for the DataFormat string array.
If e.Data.GetDataP resent("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_Ite mDrag(ByVal sender As Object, ByVal e As
System.Windows. Forms.ItemDragE ventArgs) Handles lstSelected.Ite mDrag

Dim myItem As ListViewItem
'Create an array of strings.
Dim myItems(lstSele cted.SelectedIt ems.Count - 1) As String
Dim i As Integer = 0
'Loop though the SelectedItems collection of the ListView control.
For Each myItem In lstSelected.Sel ectedItems
'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.DoD ragDrop(myItems , DragDropEffects .Move)
Dim j As ListViewItem
For Each j In lstSelected.Sel ectedItems
'Remove the ListViewItem from the ListView control.
lstSelected.Ite ms.Remove(j)
Next

End Sub

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.c om<<<<<<------
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
1018
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 across the forms.. Any help on this is highly appreciated. Regards,
17
3111
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) Me.lboxRP.SetSelected(newRPindex, True) When the last line executes, I get an error message:
3
3606
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 in the subform. Is it possible? How do I do this? Thanks in advance. Paul from Slovakia
8
2877
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 this listbox but I can't for the life of me figure out how to draw the control inside ListBox... I get as far as: private void lbImageList_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
6
2872
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 a denormalized mirror of the database, but I'm not having much luck getting the selection logic down (I haven't found a 'hook' where I can access the listbox object as an object to set the listitem's selected property before it gets rendered).. ...
7
4529
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 ASP.Net stuff when I need a Windows Form control. I've seen dual listbox populators in countless Windows applications, and have seen them run very fast, so I figured this would be extremely popular. Here's how it should work:
2
1172
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 move on their respective TSP they belong to, but prevent dragndrop of ToolStrips from one TSP to the other. How can I do that?
3
2292
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
4021
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 are not bound, I select from the bottom set and add to the top set which works fine, but now i decide to remove an item from the top set. when i tried to use a remove item code it worked fine, it did delete the item form the list but it added...
0
8459
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8378
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8577
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8653
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7398
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4202
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2786
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2018
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1783
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.