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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

intheCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.