Recent

Author Topic: How To DragNDrop txt file to load into a TListbox or a TListview ?  (Read 869 times)

Boleeman

  • Hero Member
  • *****
  • Posts: 717
How To DragNDrop txt file to load into a TListbox or a TListview ?
« on: September 22, 2023, 11:33:05 am »
Hi All.

Does anyone know of a way to load a simple text file by dragging from Explorer and onto a Tlistbox or a TListview
(by Drag and Drop as shown in the attached picture)?

Thanks in advance

I found this info in another thread but do not understand it (it is for a memo but probably intervhangeable with listbox)

https://forum.lazarus.freepascal.org/index.php?topic=43533.0

In your form set AllowDropFiles to True, either in the Object Inspector or in code
Write a handler for the form's OnDropFiles event that either loads the file(s) into the memo(s) (with Memo.LoadFromFile) or loads each file(s) into a string list and inserts it in the memo, like:
  StringList.LoadFromFile(FileName[ i ]);
  Memo.SelText := StringList.Text;
« Last Edit: September 22, 2023, 11:48:59 am by Boleeman »

Fibonacci

  • Hero Member
  • *****
  • Posts: 600
  • Internal Error Hunter
Re: How To DragNDrop txt file to load into a TListbox or a TListview ?
« Reply #1 on: September 22, 2023, 12:07:36 pm »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormDropFiles(Sender: TObject; const FileNames: array of string);
  2. var
  3.   s: string;
  4. begin
  5.   for s in FileNames do begin
  6.     with TStringStream.Create do begin
  7.       LoadFromFile(s);
  8.       ListBox1.Items.AddDelimitedText(DataString, #10, false);
  9.       Free;
  10.     end;
  11.   end;
  12. end;

paweld

  • Hero Member
  • *****
  • Posts: 1268
Re: How To DragNDrop txt file to load into a TListbox or a TListview ?
« Reply #2 on: September 22, 2023, 12:09:43 pm »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. begin
  3.   AllowDropFiles := True;
  4. end;
  5.  
  6. procedure TForm1.FormDropFiles(Sender: TObject; const FileNames: array of string);
  7. var
  8.   mpos: TPoint;
  9. begin
  10.   mpos := ScreenToClient(Mouse.CursorPos);
  11.   if ListBox1.BoundsRect.Contains(mpos) and (Length(FileNames) > 0) then //if dropped in listbox
  12.   begin
  13.     ListBox1.Items.BeginUpdate;
  14.     ListBox1.Items.Clear;
  15.     ListBox1.Items.LoadFromFile(FileNames[0]);
  16.     ListBox1.Items.EndUpdate;
  17.     if Length(FileNames) > 1 then
  18.       ShowMessage('Only the first file was loaded!');
  19.   end;
  20. end;  
Best regards / Pozdrawiam
paweld

Josh

  • Hero Member
  • *****
  • Posts: 1344
Re: How To DragNDrop txt file to load into a TListbox or a TListview ?
« Reply #3 on: September 22, 2023, 12:34:20 pm »
or

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormDropFiles(Sender: TObject; const FileNames: array of string
  2.   );
  3. Var I:Integer;
  4.     FnExt:String;
  5.     ctrl: TControl;
  6.     TS:TStringList;
  7. begin
  8.   // get control under mouse
  9.   ctrl := ControlAtPos(ScreenToClient(Mouse.CursorPos), [capfRecursive, capfAllowWinControls]);
  10.   if assigned(ctrl) then
  11.   begin
  12.     if ctrl=ListBox1 then
  13.     Begin
  14.       If High(FileNames)>=0 then
  15.       begin
  16.         For I:=Low(FileNames) to High(FileNames) do
  17.         begin
  18.           FnExt:=upcase(ExtractFileExt(FileNames[i]));
  19.           If ((FnExt='.TXT') or (FnExt='.PAS') or (FnExt='.LFM')) then
  20.           begin
  21.             ts:=TStringList.Create;
  22.             TS.LoadFromFile(FileNames[i]);
  23.             ListBox1.items.AddStrings(ts);
  24.             TS.Free;
  25.           end;
  26.         end;
  27.       end;
  28.     end;
  29.   end;
  30. end;                            
The best way to get accurate information on the forum is to post something wrong and wait for corrections.

Boleeman

  • Hero Member
  • *****
  • Posts: 717
Re: How To DragNDrop txt file to load into a TListbox or a TListview ?
« Reply #4 on: September 22, 2023, 01:19:44 pm »
Wow. Many thanks to Fibonacci, Paweld and Josh

I tried all 3 solutions.

The 1st split according to delimiter comma (so could possibly be used in a TListview), whereas the other 2 solutions put whole text line in each Listbox row.

All are great solutions.  I set AllowDropFiles to True but did not really know much of "FormDropFiles" event until I read each solution carefully.

Once again, Thank you all for helping out. I was going round in circles trying to look for a working solution.
« Last Edit: September 22, 2023, 01:21:23 pm by Boleeman »

 

TinyPortal © 2005-2018