Recent

Author Topic: Filling a listbox from a dir  (Read 6543 times)

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Filling a listbox from a dir
« on: March 02, 2019, 06:10:25 am »
I copied the following from a post and I believe   @Wp wrote the code.
I can't find the post and can't get the code to compile.

I want to pass a file name into the procedure LoadFileBox. I don't want directories or sub directories. Just the file in the specified directory.
I get an error line 40.



Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2. {$mode objfpc}{$H+}
  3. interface
  4.  
  5. uses
  6.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;
  7.  
  8. type
  9.  
  10.   { TForm1 }
  11.  
  12.   TForm1 = class(TForm)
  13.     Button1: TButton;
  14.     ListBox1: TListBox;
  15.     procedure Button1Click(Sender: TObject);
  16.   private
  17.  
  18.   public
  19.     procedure LoadFileBox(aFName: String);
  20.  
  21.   end;
  22.  
  23. var
  24.   Form1: TForm1;
  25. implementation
  26.  
  27. {$R *.lfm}
  28.  
  29. procedure TForm1.Button1Click(Sender: TObject);
  30.  Var
  31.    S : String = 'c:\folder';
  32.   begin
  33.  LoadFileBox(S);
  34. end;
  35.  
  36. procedure TForm1.LoadFileBox(aFName: String);
  37.   var
  38.   list: TStringList;
  39.   begin
  40.     list := FindAllFiles((aFName + '*' , False {don't search in subdirectory});begin
  41.     ListBox.Items := list;
  42.     list.Free;
  43.   end;
  44.   end;
  45. end.
  46.  
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Filling a listbox from a dir
« Reply #1 on: March 02, 2019, 06:33:31 am »
Probably LoadFileBox should be:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.LoadFileBox(aFName: String);
  2. var
  3.   list: TStringList;
  4. begin
  5.   list := FindAllFiles((aFName + '*' , False {don't search in subdirectory});
  6.   ListBox.Items := list;
  7.   list.Free;
  8. end;

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: Filling a listbox from a dir
« Reply #2 on: March 02, 2019, 07:01:55 am »
I get the following error.  unit1.pas(40,38) Fatal: Syntax error, ")" expected but "," found

It dosen't like the ',' after the  '*'  at line 5.


Code: Pascal  [Select][+][-]
  1. procedure TForm1.LoadFileBox(aFName: String);
  2. var
  3.   list: TStringList;
  4. begin
  5.   list := FindAllFiles((aFName + '*' , False {don't search in subdirectory});
  6.   ListBox.Items := list;
  7.   list.Free;
  8. end;
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Filling a listbox from a dir
« Reply #3 on: March 02, 2019, 07:32:38 am »
Code: Pascal  [Select][+][-]
  1.   list := FindAllFiles(aFName,'*', False {don't search in subdirectory});

Bart

  • Hero Member
  • *****
  • Posts: 5265
    • Bart en Mariska's Webstek
Re: Filling a listbox from a dir
« Reply #4 on: March 02, 2019, 12:53:05 pm »
Don't we have a TFileListBox that does exactly this?

Bart

furious programming

  • Hero Member
  • *****
  • Posts: 836
Re: Filling a listbox from a dir
« Reply #5 on: March 02, 2019, 01:57:03 pm »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.LoadFileBox(const ASearchPath, ASearchMask: String);
  2. begin
  3.   ListBox1.Items.BeginUpdate();
  4.   FindAllFiles(ListBox1.Items, ASearchPath, ASearchMask, False);
  5.   ListBox1.Items.EndUpdate();
  6. end;

Calling:

Code: Pascal  [Select][+][-]
  1. LoadFileBox('C:\Windows\', '*');

Don't we have a TFileListBox that does exactly this?

Indeed — the Directory and Mask properties allows to do the same.
« Last Edit: March 02, 2019, 02:00:46 pm by furious programming »
Lazarus 3.2 with FPC 3.2.2, Windows 10 — all 64-bit

Working solo on an acrade, action/adventure game in retro style (pixelart), programming the engine and shell from scratch, using Free Pascal and SDL. Release planned in 2026.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Filling a listbox from a dir
« Reply #6 on: March 02, 2019, 02:07:11 pm »
Don't we have a TFileListBox that does exactly this?

Bart

Yes. The difference is that while TFilelistBox shows only the file names, FindAllFiles() returns the full path+name.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Bart

  • Hero Member
  • *****
  • Posts: 5265
    • Bart en Mariska's Webstek
Re: Filling a listbox from a dir
« Reply #7 on: March 02, 2019, 02:07:17 pm »
Don't we have a TFileListBox that does exactly this?

Indeed — the Directory and Mask properties allows to do the same.

I know, I did some work on that in the past.

Bart

Bart

  • Hero Member
  • *****
  • Posts: 5265
    • Bart en Mariska's Webstek
Re: Filling a listbox from a dir
« Reply #8 on: March 02, 2019, 02:09:08 pm »
Yes. The difference is that while TFilelistBox shows only the file names, FindAllFiles() returns the full path+name.

IIRC then TFileListBox has a Directory property (I'm too lazy to look it up), so Path is known (at least to the programmer).

Bart

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Filling a listbox from a dir
« Reply #9 on: March 02, 2019, 02:44:58 pm »
IIRC then TFileListBox has a Directory property (I'm too lazy to look it up), so Path is known (at least to the programmer).

Yes, of course, but 1) It's more a matter of UI than of code; and 2) With FindAllFiles+ListBox you don't have to reconstruct each file-name if you need it. And it's not that much slower either, although, obviously, uses more memory :)

It would be otherwise if TFileListBox had an option to show fully-qualified file-names ...
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

jamie

  • Hero Member
  • *****
  • Posts: 6077
Re: Filling a listbox from a dir
« Reply #10 on: March 02, 2019, 03:45:27 pm »
There is no need to use "FindAllFiles" the FilelistBox has all that is needed in it..

you set the mask, directly etc, and call the UpdateFileList
The only true wisdom is knowing you know nothing

Bart

  • Hero Member
  • *****
  • Posts: 5265
    • Bart en Mariska's Webstek
Re: Filling a listbox from a dir
« Reply #11 on: March 02, 2019, 05:41:18 pm »
The FileName property of TFileListBox already includes the directory part.

Bart

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Filling a listbox from a dir
« Reply #12 on: March 02, 2019, 06:18:46 pm »
The FileName property of TFileListBox already includes the directory part.

Bart

Yes, for the selected file. What if you want to iterate the list doing something with each file? You could select, in code, each file-line and read FileName but then where is the advantage of TFileListBox?

And don't forget that the FileListBox is still not showing the path.

What I'm trying to say is that yes, TFileListBox is convenient and very useful for some tasks; for others, with other requirements, one could do worse than using FindAllFiles+TListBox.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Bart

  • Hero Member
  • *****
  • Posts: 5265
    • Bart en Mariska's Webstek
Re: Filling a listbox from a dir
« Reply #13 on: March 02, 2019, 06:24:00 pm »
Derive from TFileControl and make it display fully qualified names wouldn't be that hard I think.
No need to re-implement all the other logic then.

Off-topic: didn't we also have a TDriveComboBox, or is that just old Delphi?

Bart

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: Filling a listbox from a dir
« Reply #14 on: March 03, 2019, 05:39:02 pm »
I'm Using a TFileList box to display a directory's files.

Is it possible with this control to also show the sub directories in the listbox or is it only for files..
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

 

TinyPortal © 2005-2018