Search code examples
delphilistboxdelphi-xe8

Populate ListBox Using TDirectory.GetFiles Delphi XE8


Does a more efficient way of populating ListBox with file names from TDirectory.GetFiles exist?

procedure PopListBox(var lb: TListBox; dir, ext: String; so: TSearchOption);
var
  i: Integer;
  iend: Integer;
  oc: TStringDynArray;
begin
  oc := TDirectory.GetFiles(dir, ext, so);
  iend := Length(oc);
  i := 0;
  repeat
    lb.Items.Add(oc[i]);
    Inc(i);
  until (i > (iend - 1));
end;

I would like input from the community on this approach.


Solution

  • It isn't any more efficient, but you can remove a couple of variables and a few lines of code:

    procedure PopListBox(var lb: TListBox; dir, ext: String; so: TSearchOption);
    var
      oc: TStringDynArray;
      s: string;
    begin
      oc := TDirectory.GetFiles(dir, ext, so);
      lb.Items.BeginUpdate;
      try
        for s in oc do
          lb.Items.Add(s);
      finally
        lb.Items.EndUpdate;
      end;
    end;