By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,057 Members | 1,401 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,057 IT Pros & Developers. It's quick & easy.

drag and drop problem VB6

100+
P: 267
hi

i've been snooping around on the internet trying to get something usefull for my drag and drop problem.
i want to get the filename of a dropped file in a listbox.
now, i have found a source but it does not always work.
why ? or rather why not?

the code for module1

Expand|Select|Wrap|Line Numbers
  1. Type POINTAPI
  2.   x As Long
  3.   y As Long
  4. End Type
  5.  
  6. Type MSG
  7.   hWnd As Long
  8.   message As Long
  9.   wParam As Long
  10.   lParam As Long
  11.   time As Long
  12.   pt As POINTAPI
  13. End Type
  14.  
  15. Declare Sub DragAcceptFiles Lib "shell32.dll" (ByVal hWnd As Long, ByVal fAccept As Long)
  16. Declare Sub DragFinish Lib "shell32.dll" (ByVal hDrop As Long)
  17. Declare Function DragQueryFile Lib "shell32.dll" Alias "DragQueryFileA" (ByVal hDrop As Long, ByVal UINT As Long, ByVal lpStr As String, ByVal ch As Long) As Long
  18. Declare Function PeekMessage Lib "user32" Alias "PeekMessageA" (lpMsg As MSG, ByVal hWnd As Long, ByVal wMsgFilterMin As Long, ByVal wMsgFilterMax As Long, ByVal wRemoveMsg As Long) As Long
  19.  
  20. Public Const PM_NOREMOVE = &H0
  21. Public Const PM_NOYIELD = &H2
  22. Public Const PM_REMOVE = &H1
  23. Public Const WM_DROPFILES = &H233
  24.  
1 Form: frmMain
2 buttons: cmdClear and cmdExit
1 listbox: lstFiles
1 label: lblNumFiles
1 label: lblInfo ["Number of dropped files"]

Expand|Select|Wrap|Line Numbers
  1. Dim FileDropMessage As MSG      ' Msg Type
  2. Dim FileDropped As Boolean      ' True if Files where dropped
  3. Dim hDrop As Long               ' Pointer to the dropped file structure
  4. Dim FileName As String * 128    ' the dropped filename
  5. Dim numOfDroppedFiles As Long   ' the amount of dropped files
  6. Dim curFile As Long             ' the current file number
  7. Dim ret As Long
  8.  
  9. Private Sub cmdclear_Click()
  10. lstFiles.Clear
  11. lblNumFiles.Caption = "0"
  12. End Sub
  13.  
  14. Private Sub cmdexit_Click()
  15. Unload Me
  16. End
  17. End Sub
  18.  
  19. Private Sub Form_Load()
  20.  
  21. frmmain.Show
  22. DragAcceptFiles frmmain.hWnd, True
  23.  
  24. Do
  25.   If PeekMessage(FileDropMessage, 0, WM_DROPFILES, WM_DROPFILES, PM_REMOVE Or PM_NOYIELD) = True Then
  26.     hDrop = FileDropMessage.wParam
  27.     numOfDroppedFiles = DragQueryFile(hDrop, True, FileName, 127)
  28.     For curFile = 1 To numOfDroppedFiles
  29.       ret = DragQueryFile(hDrop, curFile - 1, FileName, 127)
  30.       lblNumFiles.Caption = LTrim(Str(numOfDroppedFiles))
  31.       lstFiles.AddItem FileName
  32.     Next curFile
  33.     DragFinish hDrop
  34.   End If
  35. DoEvents
  36. Loop
  37.  
  38. End Sub
  39.  
Oct 28 '06 #1
Share this Question
Share on Google+
4 Replies


Expert 5K+
P: 8,434
Would it be acceptable to drop the file onto another control, say a textbox, and have your code transfer it to the listbox?

Anyway, here's a sample I had lying around which may be some help. The files are dated 2001, I don't recall where it came from - picked it up on the web somewhere, I guess. It's just a project with a single form with the default name Form1. Here's the listing. If you want it, just create Form1, go to the code window, select all, then paste this in...
Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2. DefLng A-Z
  3.  
  4. Private Sub Text1_OLEDragOver(Data As _
  5.     DataObject, Effect As Long, Button As Integer, _
  6.     Shift As Integer, X As Single, Y As Single, State _
  7.     As Integer)
  8.   If Data.GetFormat(vbCFFiles) Then
  9.     'If the data is in the proper format, _
  10.     inform the source of the action to be taken
  11.     Effect = vbDropEffectCopy And Effect
  12.     Exit Sub
  13.   End If
  14.   'If the data is not desired format, no drop
  15.   Effect = vbDropEffectNone
  16. End Sub
  17.  
  18. Private Sub Text1_OLEDragDrop(Data As _
  19.       DataObject, Effect As Long, Button As Integer, _
  20.       Shift As Integer, X As Single, Y As Single)
  21.   If Data.GetFormat(vbCFFiles) Then
  22.     Dim vFN As Variant
  23.  
  24.     For Each vFN In Data.Files
  25.       'DropFile Text1, vFN
  26.       Text1 = Text1 & vbCrLf & vFN
  27.     Next vFN
  28.   End If
  29. End Sub
  30.  
  31. Sub DropFile(ByVal txt As TextBox, ByVal strFN$)
  32.   Dim iFile As Integer
  33.   iFile = FreeFile
  34.  
  35.   Open strFN For Input Access Read Lock Write As #iFile
  36.   Dim Str$, strLine$
  37.   While Not EOF(iFile) And Len(Str) <= 32000
  38.     Line Input #iFile, strLine$
  39.     If Str <> "" Then Str = Str & vbCrLf
  40.     Str = Str & strLine
  41.   Wend
  42.   Close #iFile
  43.  
  44.   txt.SelStart = Len(txt)
  45.   txt.SelLength = 0
  46.   txt.SelText = Str
  47.  
  48. End Sub
  49.  
Oct 30 '06 #2

100+
P: 267
hi
tnx a lot, this one works indeed.
Oct 30 '06 #3

P: 1
I just wanted to thank Killer42 for solving this same problem for me. I needed to have users drag files from Window explorer onto a form and attach them as documents based on their types. Your solution enabled me to do this.

Sincerely,
Don
Aug 7 '07 #4

Expert 5K+
P: 8,434
I just wanted to thank Killer42 for solving this same problem for me. I needed to have users drag files from Window explorer onto a form and attach them as documents based on their types. Your solution enabled me to do this.
No problem, Don. Glad we could help. :)
Aug 7 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.