Lazarus: Match FormDropFiles with a specific ListView
Tutorial by Stefan Trost | Last update on 2023-01-20 | Created on 2013-11-30
Each Form of a Lazarus program provides the handy routine FormDropFiles which makes it very easy to work with files that were dragged and dropped from any folder onto the application.
However, if you have more than one control on your form that should accept files (for example several ListViews), there is the question of which ListView has received ans should display the files. So the question is over which ListView or over which other control the user has released the mouse.
Today, I would like to introduce how to solve this problem. Here's the required code:
procedure Tprog.FormDropFiles(Sender: TObject; const FileNames: array of String); var c: TControl; i: integer; li: TListItem; begin c := FindControlAtPosition(Mouse.CursorPos, false); if c is TListView then begin for i := 0 to length(FileNames)-1 do begin li := (c as TListView).Items.Add; li.Caption := FileNames[i]; end; end else begin showmessage('Here you cannot place any files.'); end; end;
It's a quite simple concept: Just in the moment in which FormDropFiles is triggered, we are retrieving the current mouse position using Mouse.CursorPos. After that, we can use the coordinates and the function FindControlAtPosition() to determine the control just below the mouse on which the files where dropped. FindControlAtPosition() then returns the control, which we save here in the variable c.
Then we check with "if c is TListView" whether this control actually is a TListView / of type TListView, because it could also be a button or a label on which we cannot drop any files. If this is the case, we add the files to just this ListView. By the way, we should check in any case to prevent access violations, since FindControlAtPosition() can also return nil if no control has been found.
In the event that our control is not a ListView, we show a small message to the user that files cannot be dropped to this place. But of course, we can also just omit this.
Of course, the example above can also easily be transferred to any other control such as a TListBox or a Memo or any other control that should accept files. For this, we only have to check for this particular control and we have to change the part of the adding to the other control.
About the Author
You can find Software by Stefan Trost on sttmedia.com. Do you need an individual software solution according to your needs? - sttmedia.com/contact
Show Profile
Related Topics
Rename File to its Folder Name
Tutorial | 0 Comments
Delphi/Lazarus: Select all with CTRL+A in ListView
Tip | 0 Comments
MIME Types of Microsoft Office File Formats
Info | 0 Comments
Delphi: Event Order of a Form - From OnCreate to OnDestroy
Info | 1 Comment
Windows Batch Script: Computer Shutdown
Tutorial | 2 Comments
VirtualBox: Change Date and Time
Tutorial | 10 Comments
Program in ZIP-Folder
Info | 0 Comments
Important Note
Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.
Participate
Ask your own question or write your own article on askingbox.com. That’s how it’s done.