Lazarus

Free Pascal => Beginners => Topic started by: JLWest on March 02, 2019, 06:10:25 am

Title: Filling a listbox from a dir
Post by: JLWest 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.  
Title: Re: Filling a listbox from a dir
Post by: engkin 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;
Title: Re: Filling a listbox from a dir
Post by: JLWest 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;
Title: Re: Filling a listbox from a dir
Post by: engkin on March 02, 2019, 07:32:38 am
Code: Pascal  [Select][+][-]
  1.   list := FindAllFiles(aFName,'*', False {don't search in subdirectory});
Title: Re: Filling a listbox from a dir
Post by: Bart on March 02, 2019, 12:53:05 pm
Don't we have a TFileListBox that does exactly this?

Bart
Title: Re: Filling a listbox from a dir
Post by: furious programming 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.
Title: Re: Filling a listbox from a dir
Post by: lucamar 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.
Title: Re: Filling a listbox from a dir
Post by: Bart 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
Title: Re: Filling a listbox from a dir
Post by: Bart 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
Title: Re: Filling a listbox from a dir
Post by: lucamar 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 ...
Title: Re: Filling a listbox from a dir
Post by: jamie 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
Title: Re: Filling a listbox from a dir
Post by: Bart on March 02, 2019, 05:41:18 pm
The FileName property of TFileListBox already includes the directory part.

Bart
Title: Re: Filling a listbox from a dir
Post by: lucamar 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.
Title: Re: Filling a listbox from a dir
Post by: Bart 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
Title: Re: Filling a listbox from a dir
Post by: JLWest 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..
Title: Re: Filling a listbox from a dir
Post by: lucamar on March 03, 2019, 06:13:54 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..

Yes, it's possible. Set FileListBox.FileType to FileListBox.FileType + [ftDirectory]; either in code or in the Object Inspector. Or viceversa to not show them.
Title: Re: Filling a listbox from a dir
Post by: JLWest on March 03, 2019, 06:39:31 pm
Thanks
Title: Re: Filling a listbox from a dir
Post by: lucamar on March 03, 2019, 07:36:58 pm
I know you'll probably not need it but just in case (and because I was bored :)) I've prepared a basic mini-demo of FileListBox. Have fun!
Title: Re: Filling a listbox from a dir
Post by: JLWest on March 03, 2019, 11:40:08 pm
Thanks I'll try it out.
TinyPortal © 2005-2018