Drag and Drop With List Box

Level:
Level1

Drag and Drop is an amazing feature to add to your application when you are trying to create a slick and intuitive interface for your end users. It also adds a wow factor that impresses people when they first start using an application. Another great benefit of adding drag and drop functionality to your application is that it is very logical for the user. In real life if I want to move a piece of paper from one stack to another I simply pick it up, move it, and place it in the new stack. This is the exact same gesture I go through with Drag and Drop.

To use this source sample create a new VB project, add a listbox control to the form and set its DragMode property to Automatic. Go to the code window and add the following code.

  1. Dim ItemCount As Integer
  2. Dim I As Integer
  3.  
  4. On Error Resume Next
  5. ItemCount = Data.Files.Count
  6.  
  7. For I = 1 To ItemCount
  8.     List1.AddItem Data.Files(I)
  9. Next
  10.  
  11. If Err Then Err.Clear

Once you have the code in place run your program (Press F5) and then you can drag things into your list box. For example open up your My Computer explorer and drag a folder from it onto your listbox. You will see it now is located in the box. You can build upon this to add great drag and drop support to your applications.

Note: The source for this was found at DreamVB which is no longer online.

If you enjoyed this post, subscribe for updates (it's free)

thank you very much . . .

thank you very much . . .

HI

None of this works ... :/

Private Sub Form_Load()

List1.DragMode = vbAutomatic

List1.OLEDragMode = vbAutomatic

List1.OLEDropMode = vbManual

End Sub

Private Sub Form_OLEDragDrop(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim ItemCount As Integer
Dim I As Integer

' On Error Resume Next
ItemCount = Data.Files.Count
For I = 1 To ItemCount
List1.AddItem Data.Files(I)
Next
If Err Then Err.Clear
End Sub

Just got solution ..

turn dropmode to manual ... and It works ..

drag and drop in listbox

Hi there everyone....

I think that DragMode, OleDragMode and OleDropMode must be set to Manual for the ListBox to be click-able. I set them all to Manual and it went fine......

how to drag an image in an

how to drag an image in an image box?

code

Here is my code, it works:

Private Sub Form_Load()

'a listbox control in the form and set its DragMode property to Automatic
List1.DragMode = vbAutomatic

List1.OLEDragMode = vbAutomatic

List1.OLEDropMode = vbAutomatic

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 ItemCount As Integer
Dim I As Integer
On Error Resume Next
ItemCount = Data.Files.Count

For I = 1 To ItemCount
List1.AddItem Data.Files(I)
Next

If Err Then Err.Clear
End Sub

BazinGa

This works .. :)

I have tried many...many

I have tried many...many examples and none of them, including this one works! What am I doing wrong? I followed instructions to the letter and none of them work!

description is incomplete

you have to do the following:
- set oledragmode to auto
- set oledropmode to manual
- put the provided code in Sub List1_OLEDragDrop(...)

regards,
ekrem.

I followed your

I followed your instructions...still not working!

hey dude ! you set the

hey dude !

you set the listbox into dragmode?

if not set it and double click the listbox then
follow the codes

Hi there, I'm a real

Hi there,

I'm a real beginner to this thing,
i typed everything that is displayed above and even copied and paste it
( and yes, i deleted the numbers 1,2,3...)
but it still wouldn't run
it highlighted 'On Error Resume Next'
saying that its an invalid outside procedure..
what does that mean??

See this

See this link:
http://www.developerfusion.com/code/154/drag-and-drop-listbox-items/

Note: once you are comfortable with the project, look at the definition of the constants vbDropEffectMove & vbCFText for more advanced drag and drop options

Hi there beginner, I think

Hi there beginner,

I think you just pasted the code in VB6 and then run it
but you have to put in a 'sub' like described above ---> Sub List1_OLEDragDrop(...)
then try it,btw the dim's that are before 'on error resume next' are also not in a sub
but vb6 understands them because you can also declare variables in the top out of a sub.

Hope this will help you