We're working hard on the next version of Developer Fusion. Let us know what you think
we should be up to!
Members
Technology Zones
Articles
Hosted By
Info
|
Rated
Read 49,873 times
Related Categories
Drag and Drop ListBox items
This example shows you how to enable the user to drop and drag items
between two list boxes. Add two listboxes to your form, and insert
the following code.
Private Sub Form_Load()
' Populate the list
List1.AddItem "James"
List1.AddItem "Frederick"
List1.AddItem "Ann"
List1.AddItem "Paul"
List1.AddItem "Sarah"
List1.OLEDropMode = 1
List2.OLEDropMode = 1
End Sub
' Code managing dropping from list one
' to list two
Private Sub List1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y
As Single)
List1.OLEDrag ' Begin dragging
End Sub
Private Sub List1_OLEStartDrag(Data As DataObject, AllowedEffects As Long)
' Only allow moves
AllowedEffects = vbDropEffectMove
' Assign the ListBox selection to the DataObject
Data.SetData List1
End Sub
Private Sub List2_OLEDragDrop(Data As DataObject, Effect As Long, Button As
Integer, Shift As Integer, X As Single, Y As Single)
Dim strList As String
' Check the format of the DataObject
If Not Data.GetFormat(vbCFText) Then Exit Sub
' Retrieve the text from the DataObject
strList = Data.GetData(vbCFText)
' If the item was not dropped on itself
If Not strList = List2.Text Then
List2.AddItem strList
'Remove the item from the ListBox
List1.RemoveItem List1.ListIndex
End If
End Sub
''
''
'' Code managing dropping from list one
'' to list two
''
Private Sub List2_MouseDown(Button As Integer, Shift As Integer, X As Single, Y
As Single)
List2.OLEDrag ' Begin dragging
End Sub
Private Sub List2_OLEStartDrag(Data As DataObject, AllowedEffects As Long)
' Only allow moves
AllowedEffects = vbDropEffectMove
' Assign the ListBox selection to the DataObject
Data.SetData List2
End Sub
Private Sub List1_OLEDragDrop(Data As DataObject, Effect As Long, Button As
Integer, Shift As Integer, X As Single, Y As Single)
Dim strList As String
' Check the format of the DataObject
If Not Data.GetFormat(vbCFText) Then Exit Sub
' Retrieve the text from the DataObject
strList = Data.GetData(vbCFText)
' If the item was not dropped on itself
If Not strList = List1.Text Then
List1.AddItem strList
'Remove the item from the ListBox
List2.RemoveItem List2.ListIndex
End If
End Sub
James first started writing tutorials on Visual Basic in 1999 whilst starting this website (then known as VB Web). Since then, the site has grown rapidly, and James has written numerous tutorials, articles and reviews on VB, PHP, ASP and C#. In October 2003, James formed the company Developer Fusion Ltd, which owns this website, and also offers various development services. In his spare time, he's a 3rd year undergraduate studying Computer Science in the UK. He's also a Visual Basic MVP.
Comments
-
Posted by Ward on 12 Jan 2006
Hello,
I've been trying to adjust this code for listbox - label drag dropping, changed some things of course, but
it doesn't seem to work.
Can anyone help me ?
thx -
Posted by paulj on 21 Apr 2005
Trying to use this for listboxes in VBA. is there a specific reference library I may be missing/needed for this to work? -
Posted by paff on 12 Nov 2003
This works very fine for me.
Thanks a lot
paff -
Posted by mikpod on 03 Oct 2003
Nice work! Thanks James. Why not extend the code to allow for any number of listboxes by creating a global control variable (set in MouseDown sub) which would identify the source list for the RemoveI...
|
Search
Code Samples
New Members
|