Recent

Author Topic: FindAllFiles function and Listbox1  (Read 1703 times)

mpknap

  • Full Member
  • ***
  • Posts: 155
FindAllFiles function and Listbox1
« on: August 16, 2020, 09:10:21 pm »
I get the names of the files from a folder using the FindAllFiles function, display them in listbox1. It's easy.
I have a problem how to do that after clicking on a file name in LISTBOX, the PATH for this file will be displayed


Code: Pascal  [Select][+][-]
  1.  
  2. GetDir(0, S);
  3. ChDir(s + '\credo-data-export\detections');
  4.  
  5.   jsonFiles := FindAllFiles('', '*.json', False);
  6.   try
  7.      listbox1.Items := jsonFiles;
  8.   finally
  9.   jsonFiles.Free;
  10.  
  11.   end;
  12.  ChDir(s);      

Handoko

  • Hero Member
  • *****
  • Posts: 5158
  • My goal: build my own game engine using Lazarus
Re: FindAllFiles function and Listbox1
« Reply #1 on: August 16, 2020, 09:22:42 pm »
You can try TFileListBox, which can be found in the Misc tab. And then you can set the Directory and Mask properties to what you want.

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, Forms, Controls, FileCtrl, StdCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     FileListBox1: TFileListBox;
  17.     procedure Button1Click(Sender: TObject);
  18.   private
  19.   end;
  20.  
  21. var
  22.   Form1: TForm1;
  23.  
  24. implementation
  25.  
  26. {$R *.lfm}
  27.  
  28. { TForm1 }
  29.  
  30. procedure TForm1.Button1Click(Sender: TObject);
  31. begin
  32.   FileListBox1.Directory := '/Data';
  33.   FileListBox1.Mask := '*.pas';
  34. end;
  35.  
  36. end.

mpknap

  • Full Member
  • ***
  • Posts: 155
Re: FindAllFiles function and Listbox1
« Reply #2 on: August 16, 2020, 09:37:36 pm »
Thanks . I'll take advantage of this
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, Forms, Controls, FileCtrl, StdCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     FileListBox1: TFileListBox;
  17.     Label1: TLabel;
  18.     procedure Button1Click(Sender: TObject);
  19.     procedure FileListBox1Click(Sender: TObject);
  20.   private
  21.   end;
  22.  
  23. var
  24.   Form1: TForm1;
  25.  
  26. implementation
  27.  
  28. {$R *.lfm}
  29.  
  30. { TForm1 }
  31.  
  32. procedure TForm1.Button1Click(Sender: TObject);
  33. begin
  34.   FileListBox1.Directory := 'C:\DataCenter\PythonScript';
  35.   FileListBox1.Mask := '*.pas';
  36. end;
  37.  
  38. procedure TForm1.FileListBox1Click(Sender: TObject);
  39. begin
  40.    label1.Caption:=FileListBox1.Directory;
  41. end;
  42.  
  43. end.

Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Re: FindAllFiles function and Listbox1
« Reply #3 on: August 16, 2020, 09:49:04 pm »
Code: Pascal  [Select][+][-]
  1.  
  2. GetDir(0, S);
  3. ChDir(s + '\credo-data-export\detections');
  4.  
  5.   jsonFiles := FindAllFiles('', '*.json', False);
  6.  
No need for the ChDir's.
Just do a FindAllFiles('.\credo-data-export\detections');
GetDir(0,S) will get you the current directory, and so wil prepending the path with '.\'.

Bart

 

TinyPortal © 2005-2018