Recent

Author Topic: Filling a listbox with files in a folder  (Read 8979 times)

torbente

  • Sr. Member
  • ****
  • Posts: 325
    • Noso Main Page
Filling a listbox with files in a folder
« on: March 31, 2015, 02:34:30 pm »
How i could fill a listbox with, for example, files '*.ctp' in a folder?

Any help is more than welcome...
Noso Cryptocurrency Main Developer
https://github.com/DevTeamNoso/NosoWallet

madref

  • Hero Member
  • *****
  • Posts: 949
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: Filling a listbox with files in a folder
« Reply #1 on: March 31, 2015, 02:42:03 pm »
Read the filenames and add them to the listbox
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Lazarus 3.99 (rev main_3_99-649-ge13451a5ab) FPC 3.3.1 x86_64-darwin-cocoa
Mac OS X Monterey

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Filling a listbox with files in a folder
« Reply #2 on: March 31, 2015, 02:50:42 pm »
Code: [Select]
var
  Directory, Mask: String;
  sr: TSearchRec;
begin
  Directory := 'c:\some\directory\';
  Mask := '*.ctp';
  if FindFirstUTF8(Directory+Mask,faAnyFile,sr)=0 then
    repeat
      YourListBox.Items.Add(sr.Name);
    until FindNextUTF8(sr)<>0;
  FindCloseUTF8(sr);
end;

paweld

  • Hero Member
  • *****
  • Posts: 970
Re: Filling a listbox with files in a folder
« Reply #3 on: March 31, 2015, 03:31:41 pm »
Or
Code: [Select]
ListBox1.Items:=FindAllFiles('c:\folder', '*.ctp', true {search in subdirectory});
Best regards / Pozdrawiam
paweld

wp

  • Hero Member
  • *****
  • Posts: 11858
Re: Filling a listbox with files in a folder
« Reply #4 on: March 31, 2015, 03:45:26 pm »
Or
Code: [Select]
ListBox1.Items:=FindAllFiles('c:\folder', '*.ctp', true {search in subdirectory});

This is a beautiful example why I don't like the FindAllFiles function and this construction. The function creates a StringList into which it copies the file names. The assignment of this list to the Listbox.Items does not assign the pointer, but performs a string-wise copy into the Listbox's items. This means that the StringList created by the FindAllFiles is no longer accessible, cannot be destroyed and leaves a memory leak (see http://bugs.freepascal.org/view.php?id=27054).

The correct way, in my eyes, would be
Code: [Select]
var
  list: TStringList;
begin
  list := FindAllFiles(('c:\folder', '*.ctp', true {search in subdirectory});
  List.Items := list;
  // or better (since is does not give the impression that a pointer is copied):
  // List.Items.Assign(list);
  list.Free;
end;

EDIT: The last code fragment is bull-sh... The upper-case variable "List" should be named "ListBox" - Pascal is not case-sensitive:
Code: [Select]
var
  list: TStringList;
begin
  list := FindAllFiles(('c:\folder', '*.ctp', true {search in subdirectory});
  ListBox.Items := list;
  // or better (since is does not give the impression that a pointer is copied):
  // ListBox.Items.Assign(list);
  list.Free;
end;
« Last Edit: September 13, 2018, 07:00:30 pm by wp »

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: Filling a listbox with files in a folder
« Reply #5 on: March 31, 2015, 04:39:08 pm »
TFileListBox?

Bart

Birger52

  • Sr. Member
  • ****
  • Posts: 309
Re: Filling a listbox with files in a folder
« Reply #6 on: September 13, 2018, 06:49:55 pm »
Old topic.
Have a question, tho:

The correct way, in my eyes, would be
Code: [Select]
var
  list: TStringList;
begin
  list := FindAllFiles(('c:\folder', '*.ctp', true {search in subdirectory});
  List.Items := list;
  // or better (since is does not give the impression that a pointer is copied):
  // List.Items.Assign(list);
  list.Free;
end;

If the pointer to list is freed like this, will it not delete the data in the list - and thus the strings in the listbox.items?
Lazarus 2.0.8 FPC 3.0.4
Win7 64bit
Playing and learning - strictly for my own pleasure.

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Filling a listbox with files in a folder
« Reply #7 on: September 13, 2018, 06:52:21 pm »
Old topic.
Have a question, tho:

The correct way, in my eyes, would be
Code: [Select]
var
  list: TStringList;
begin
  list := FindAllFiles(('c:\folder', '*.ctp', true {search in subdirectory});
  List.Items := list;
  // or better (since is does not give the impression that a pointer is copied):
  // List.Items.Assign(list);
  list.Free;
end;

If the pointer to list is freed like this, will it not delete the data in the list - and thus the strings in the listbox.items?
no. See for your self. Ctrl+Click the listbox.items property ctrl+click the write method name in that line and then read the code there.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

ASerge

  • Hero Member
  • *****
  • Posts: 2223
Re: Filling a listbox with files in a folder
« Reply #8 on: September 14, 2018, 03:57:22 am »
This is a beautiful example why I don't like the FindAllFiles function and this construction.
You're right, but there is procedure form of this function.
Code: Pascal  [Select][+][-]
  1. FindAllFiles(List.Items, 'c:\folder', '*.ctp', True);

 

TinyPortal © 2005-2018