473,402 Members | 2,050 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

drag and drop problem VB6

267 100+
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
4 5167
Killer42
8,435 Expert 8TB
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
albertw
267 100+
hi
tnx a lot, this one works indeed.
Oct 30 '06 #3
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
Killer42
8,435 Expert 8TB
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

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

Similar topics

6
by: hemant_mishal | last post by:
Hi All, I am writting a java script library which will provide inter frame drag and drop operation. The more precise will be OLE drag and drop, which means only data will be transfered from frame...
0
by: Lauren Quantrell | last post by:
I'm trying to drop a file from Windows Explorer (or desktop, etc.) onto a field in Access2K and capture the full file path. I found an posting below that says this is possible but I cannot...
2
by: SamSpade | last post by:
There seems to be two ways to put things on the clipboard ( I don't mean different formats): SetClipboardData and OleSetClipboard If I want to get data off the clipboard do I care how it was put...
3
by: Ajay Krishnan Thampi | last post by:
I have a slight problem implementing 'drag and drop' from a datagrid to a tree-view. I have pasted my code below. Someone please advice me on what to do...pretty blur right now. ==code== ...
6
by: jojobar | last post by:
Hello, I look at the asp.net 2.0 web parts tutorial on the asp.net web site. I tried to run it under firefox browser but it did not run. If I want to use this feature in a commercial product...
3
by: VB Programmer | last post by:
In VB.NET 2005 (winform) any sample code to drag & drop items between 2 listboxes? Thanks!
0
by: ViRi | last post by:
I am currently experimenting a bit with AxMicrosoft.MediaPlayer.Intero­p.AxWindowsMediaPlayer and so far, most has gone well. Currently, i would like to add drag-and-drop functionality to the...
1
by: Darren | last post by:
I'm trying to create a file using drag and drop. I want to be able to select a listview item drag it to the shell and create a file. Each icon in the listview represents a blob in a database. When...
0
by: RHSFSS | last post by:
Hi, I have a Drag and Drop registration problem (See http://www.thescripts.com/forum/thread434707.html for similar problem post), can anyone out thereadvise on the best solution? I have a .NET 2.0 ...
5
by: Romulo NF | last post by:
Greetings, I´m back here to show the new version of the drag & drop table columns (original script ). I´ve found some issues with the old script, specially when trying to use 2 tables with...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
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...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.