Forum > Beginners

How To DragNDrop txt file to load into a TListbox or a TListview ?

(1/1)

Boleeman:
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;

Fibonacci:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.FormDropFiles(Sender: TObject; const FileNames: array of string);var  s: string;begin  for s in FileNames do begin    with TStringStream.Create do begin      LoadFromFile(s);      ListBox1.Items.AddDelimitedText(DataString, #10, false);      Free;    end;  end;end;

paweld:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.FormCreate(Sender: TObject);begin  AllowDropFiles := True;end; procedure TForm1.FormDropFiles(Sender: TObject; const FileNames: array of string);var  mpos: TPoint;begin  mpos := ScreenToClient(Mouse.CursorPos);  if ListBox1.BoundsRect.Contains(mpos) and (Length(FileNames) > 0) then //if dropped in listbox  begin    ListBox1.Items.BeginUpdate;    ListBox1.Items.Clear;    ListBox1.Items.LoadFromFile(FileNames[0]);    ListBox1.Items.EndUpdate;    if Length(FileNames) > 1 then      ShowMessage('Only the first file was loaded!');  end;end;  

Josh:
or


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.FormDropFiles(Sender: TObject; const FileNames: array of string  );Var I:Integer;    FnExt:String;    ctrl: TControl;    TS:TStringList;begin  // get control under mouse  ctrl := ControlAtPos(ScreenToClient(Mouse.CursorPos), [capfRecursive, capfAllowWinControls]);  if assigned(ctrl) then  begin    if ctrl=ListBox1 then    Begin      If High(FileNames)>=0 then      begin        For I:=Low(FileNames) to High(FileNames) do        begin          FnExt:=upcase(ExtractFileExt(FileNames[i]));          If ((FnExt='.TXT') or (FnExt='.PAS') or (FnExt='.LFM')) then          begin            ts:=TStringList.Create;            TS.LoadFromFile(FileNames[i]);            ListBox1.items.AddStrings(ts);            TS.Free;          end;        end;      end;    end;  end;end;                            

Boleeman:
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.

Navigation

[0] Message Index

Go to full version