Re: multiple filenames in Delphi label.caption


Posted by webmaster Guido on September 01, 2000 at 22:14:47:

In Reply to: multiple filenames in Delphi label.caption posted by Ken on August 30, 2000 at 16:06:08:

: I need a text string containing the names of multiple files (selected by clicking on a filelistbox).
: Another possibility is that the names are all copied to 1 .txt file.
---
Supposing a filelistbox named FLB1 and with the property MultiSelect set to TRUE. To have all the selected filenames in one string, separated by spaces:

procedure TForm1.Button1Click(Sender:  TObject);
var
  i: integer;
  S: string;
begin
  S := '';
  for i := 0 to (FLB1.Items.Count - 1) do
    if FLB1.Selected[i] then begin
      S := S + FLB1.Items[i] + ' ';
      // Do something with S
      Label1.Caption := S;
    end;  
end;

To save the selected filenames to a text file, for example FILES.TXT in the application's directory:

procedure TForm1.Button2Click(Sender: TObject);
const
  TextFileName = 'files.txt';
var
  i: integer;
  SL: TStringList;
  Dir: string;
begin
  SL := TStringList.Create;
  for i := 0 to (FLB1.Items.Count - 1) do
    if FLB1.Selected[i] then
      SL.Add(FLB1.Items[i]);
  Dir := ExtractFileDir(Application.ExeName);
  if Dir[Length(Dir)] <> '\' then Dir := Dir + '\';
  SL.SaveToFile(Dir + TextFileName);
  SL.Free;
end;  

 


Related Articles and Replies: