The following code will allow you to drag and drop items between two listboxs It copys the item from one listbox to another then deletes it from the source listbox were it is copyed from.
What is happing if you click twice on a item in a list box it makes a copy in the list box. I do not desire this effect.
Can anybody help with code for this.
Thanks
Jamie
Private Sub UserForm_Initialize()
'add the numbers 1 to 3 to each listbox
Dim i As Long
For i = 1 To 3
ListBox1.AddItem i
ListBox2.AddItem i
Next i
End Sub
'ListBox Control, DataObject Object, MouseMove Event, StartDrag, SetText Methods Example
'The following example demonstrates a drag-and-drop operation from one
'ListBox to another using a DataObject to contain the dragged text.
'This code sample uses the SetText and StartDrag methods in the MouseMove event
'to implement the drag-and-drop operation.
'To use this example, copy this sample code to the Declarations portion of a form.
'Make sure that the form contains two ListBox controls named ListBox1 and ListBox2.
Private Sub ListBox1_BeforeDropOrPaste(ByVal Cancel As MSForms.ReturnBoolean, _
ByVal Action As Long, _
ByVal Data As MSForms.DataObject, _
ByVal X As Single, _
ByVal Y As Single, _
ByVal Effect As MSForms.ReturnEffect, _
ByVal Shift As Integer)
Dim i As Long
Cancel = True
Effect = 1
ListBox1.AddItem Data.GetText
With Me.ListBox2
For i = 1 To .ListCount
If .List(i - 1, 0) = Data.GetText Then
.RemoveItem i - 1
Exit For
End If
Next i
End With
End Sub
Private Sub ListBox2_BeforeDropOrPaste(ByVal Cancel As MSForms.ReturnBoolean, _
ByVal Action As Long, _
ByVal Data As MSForms.DataObject, _
ByVal X As Single, _
ByVal Y As Single, _
ByVal Effect As MSForms.ReturnEffect, _
ByVal Shift As Integer)
Dim i As Long
Cancel = True
Effect = 1
ListBox2.AddItem Data.GetText
With Me.ListBox1
For i = 1 To .ListCount
If .List(i - 1, 0) = Data.GetText Then
.RemoveItem i - 1
Exit For
End If
Next i
End With
End Sub
Private Sub ListBox1_BeforeDragOver(ByVal Cancel As MSForms.ReturnBoolean, _
ByVal Data As MSForms.DataObject, _
ByVal X As Single, _
ByVal Y As Single, _
ByVal DragState As Long, _
ByVal Effect As MSForms.ReturnEffect, _
ByVal Shift As Integer)
Cancel = True
Effect = 1
End Sub
Private Sub ListBox2_BeforeDragOver(ByVal Cancel As MSForms.ReturnBoolean, _
ByVal Data As MSForms.DataObject, _
ByVal X As Single, _
ByVal Y As Single, _
ByVal DragState As Long, _
ByVal Effect As MSForms.ReturnEffect, _
ByVal Shift As Integer)
Cancel = True
Effect = 1
End Sub
Private Sub ListBox1_MouseMove(ByVal Button As Integer, _
ByVal Shift As Integer, _
ByVal X As Single, _
ByVal Y As Single)
Dim MyDataObject As DataObject
Dim Effect As Integer
If Button = 1 Then
Set MyDataObject = New DataObject
If Me.ListBox1.Text = "" Then
Exit Sub
End If
MyDataObject.SetText ListBox1.Value
Effect = MyDataObject.StartDrag
End If
End Sub
Private Sub ListBox2_MouseMove(ByVal Button As Integer, _
ByVal Shift As Integer, _
ByVal X As Single, _
ByVal Y As Single)
Dim MyDataObject As DataObject
Dim Effect As Integer
If Button = 1 Then
Set MyDataObject = New DataObject
If Me.ListBox2.Text = "" Then
Exit Sub
End If
MyDataObject.SetText ListBox2.Value
Effect = MyDataObject.StartDrag
End If
End Sub
What is happing if you click twice on a item in a list box it makes a copy in the list box. I do not desire this effect.
Can anybody help with code for this.
Thanks
Jamie
Private Sub UserForm_Initialize()
'add the numbers 1 to 3 to each listbox
Dim i As Long
For i = 1 To 3
ListBox1.AddItem i
ListBox2.AddItem i
Next i
End Sub
'ListBox Control, DataObject Object, MouseMove Event, StartDrag, SetText Methods Example
'The following example demonstrates a drag-and-drop operation from one
'ListBox to another using a DataObject to contain the dragged text.
'This code sample uses the SetText and StartDrag methods in the MouseMove event
'to implement the drag-and-drop operation.
'To use this example, copy this sample code to the Declarations portion of a form.
'Make sure that the form contains two ListBox controls named ListBox1 and ListBox2.
Private Sub ListBox1_BeforeDropOrPaste(ByVal Cancel As MSForms.ReturnBoolean, _
ByVal Action As Long, _
ByVal Data As MSForms.DataObject, _
ByVal X As Single, _
ByVal Y As Single, _
ByVal Effect As MSForms.ReturnEffect, _
ByVal Shift As Integer)
Dim i As Long
Cancel = True
Effect = 1
ListBox1.AddItem Data.GetText
With Me.ListBox2
For i = 1 To .ListCount
If .List(i - 1, 0) = Data.GetText Then
.RemoveItem i - 1
Exit For
End If
Next i
End With
End Sub
Private Sub ListBox2_BeforeDropOrPaste(ByVal Cancel As MSForms.ReturnBoolean, _
ByVal Action As Long, _
ByVal Data As MSForms.DataObject, _
ByVal X As Single, _
ByVal Y As Single, _
ByVal Effect As MSForms.ReturnEffect, _
ByVal Shift As Integer)
Dim i As Long
Cancel = True
Effect = 1
ListBox2.AddItem Data.GetText
With Me.ListBox1
For i = 1 To .ListCount
If .List(i - 1, 0) = Data.GetText Then
.RemoveItem i - 1
Exit For
End If
Next i
End With
End Sub
Private Sub ListBox1_BeforeDragOver(ByVal Cancel As MSForms.ReturnBoolean, _
ByVal Data As MSForms.DataObject, _
ByVal X As Single, _
ByVal Y As Single, _
ByVal DragState As Long, _
ByVal Effect As MSForms.ReturnEffect, _
ByVal Shift As Integer)
Cancel = True
Effect = 1
End Sub
Private Sub ListBox2_BeforeDragOver(ByVal Cancel As MSForms.ReturnBoolean, _
ByVal Data As MSForms.DataObject, _
ByVal X As Single, _
ByVal Y As Single, _
ByVal DragState As Long, _
ByVal Effect As MSForms.ReturnEffect, _
ByVal Shift As Integer)
Cancel = True
Effect = 1
End Sub
Private Sub ListBox1_MouseMove(ByVal Button As Integer, _
ByVal Shift As Integer, _
ByVal X As Single, _
ByVal Y As Single)
Dim MyDataObject As DataObject
Dim Effect As Integer
If Button = 1 Then
Set MyDataObject = New DataObject
If Me.ListBox1.Text = "" Then
Exit Sub
End If
MyDataObject.SetText ListBox1.Value
Effect = MyDataObject.StartDrag
End If
End Sub
Private Sub ListBox2_MouseMove(ByVal Button As Integer, _
ByVal Shift As Integer, _
ByVal X As Single, _
ByVal Y As Single)
Dim MyDataObject As DataObject
Dim Effect As Integer
If Button = 1 Then
Set MyDataObject = New DataObject
If Me.ListBox2.Text = "" Then
Exit Sub
End If
MyDataObject.SetText ListBox2.Value
Effect = MyDataObject.StartDrag
End If
End Sub