Listbox Items

Hello,
I need some help on the following:
How would you load a list of files from
a current directory with a certain extension..Eg. myfile1.adr, myfile2.adr
etc.into a listbox and then by selecting that item from the listbox (in the listbox) and load
the files contents into a memobox for
example.(note) file types are of text.
Regards,
frwAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
intheConnect With a Mentor Commented:
hi,
you can do it like:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    ListBox1: TListBox;
    Button1: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
    procedure ListBox1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure GatherFiles(Const Lines: TStrings; Const Dir,Match: String);
var
  Rec: TSearchRec;
  Found: Integer;
Begin
  // Add files
  Found:= FindFirst(Dir+'\'+Match, faAnyFile, Rec);
  While Found = 0 do
  begin
    If (Rec.Attr AND faDirectory) = 0 Then Lines.Add(Dir+'\'+Rec.Name);
    Found := FindNext(Rec);
  End;
  FindClose(Rec);
  // Find directories (Without match)
  Found:= FindFirst(Dir+'\*.*', faAnyFile, Rec);
  While Found = 0 do
  begin
    If ((Rec.Attr AND faDirectory)>0) AND
       (Rec.Name<>'.') AND
       (Rec.Name<>'..') Then GatherFiles(Lines,Dir+'\'+Rec.Name,Match);
    Found := FindNext(Rec);
  End;
  FindClose(Rec);
End;


procedure TForm1.Button1Click(Sender: TObject);
begin
Listbox1.items.Clear;
  GatherFiles(ListBox1.Items,'C:','*.txt');
end;

procedure TForm1.ListBox1Click(Sender: TObject);
var
i : integer;
begin
i := listbox1.ItemIndex;
memo1.Lines.loadfromfile(listbox1.Items.Strings[i])

end;

end.


Regards Barry
0
 
intheCommented:
hi
just a note,
you must have pressed post twice for this question and 8 hours after i answered this one someone has gone and answered the duplicate of this question even though i left a comment in it saying it was a duplicate .
if you accept both answers you'll lose 200 points:-( so best thing is decide which answer suits best and accept that one and then get customer services to delete the other duplicate so you get your 200 points back.
Regards Barry
0
 
ianBCommented:
frw ,

You have posted this question twice, you need to decide which question you want to keep and I will close the other one.

Please see :

http://www.experts-exchange.com/jsp/qShow.jsp?ta=commspt&qid=10253734 

when you next see this comment.

Ian
COmmunity Support @ Experts Exchange
0
 
frwAuthor Commented:
Sorry guys must have not been paying
attention.
0
All Courses

From novice to tech pro — start learning today.