22 Votes

Lazarus: Match FormDropFiles with a specific ListView

Tutorial by Delphian | 2013-11-30 at 20:48

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 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 the files.

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.

Then we check whether this control actually is a 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.

In the event that our control is not a ListView, we show a small message 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.

ReplyPositiveNegative

About the Author

AvatarThe author has not added a profile short description yet.
Show Profile

 

Related Topics

Change Date of File

Tutorial | 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.