我想点击一个按钮,然后查找“.txt”文件,文件名显示在一个 ListBox 或FileListBox里,然后点击 ListBox 或FileListBox里某个文件,然后把文件内容显示在下面的Memo里咋办?该怎么做呢?
很感谢!

解决方案 »

  1.   

    var sr: TSearchRec;
    begin
      if FindFirst("c:\*.txt", faAnyFile, sr) = 0 then
      repeat
        ListBox.Items.Add(sr.Name);
      until FindNext(sr) <> 0;
      FindClose(sr);
    end;
      

  2.   

    用TFileListBox简直太简单了:
     FileListBox1->Mask="*.txt";
    FileListBox1->Directory = "YourDirectory";然后在TFileListBox的事件中Memo1->Lines->LoadFromLie("the file name");
      

  3.   

    我是在程序中控制的,
    要找的并不只是.txt文件,还有其他文件,
    我列出来只是示范一下,谢谢
    westfly(西翔)的有点问题啊。
    [Warning] tmouse.pas(7): Unit 'FileCtrl' is specific to a platform
    [Error] tmouse.pas(144): Statement expected but 'VAR' found
    [Error] tmouse.pas(148): Undeclared identifier: 'ListBox1'
    [Error] tmouse.pas(148): Missing operator or semicolon
    [Error] tmouse.pas(151): '.' expected but ';' found
    [Warning] tmouse.pas(152): Text after final 'END.' - ignored by compiler
    [Fatal Error] Project1.dpr(5): Could not compile used unit 'tmouse.pas'
      

  4.   

    用这个,能找到指定目录下的任何文件,支持通配符。
    function seekfile(const dir,fType:string;var flist:tstrings):boolean;
    var
      searchRec:TSearchRec;
      fDir:string;
    begin
    fDir:=dir;
    if pos('\',fdir)=length(fdir) then delete(fdir,length(fdir),1);
    if not DirectoryExists(fdir) then
      begin
        result:=false;
      end
      else begin
        FindFirst(fdir+'\*.*',faAnyfile,SearchRec);
        repeat
          if (SearChRec.Attr and faDirectory>0) then
              begin
                if (SearchRec.Name[1]<>'.') and (SearchRec.Name<>'RECYCLER') then
                  begin
                    if not seekfile(fdir+'\'+SearchRec.Name,ftype,flist) then
                       break;
                  end;
              end//是目录
              else begin
                if fType='*.*' then
                  flist.Add(fdir+'\'+searchrec.Name);
                if (fType[1]='*') and (fType[3]<>'*') then
                if extractfileext(searchrec.Name)=fType then
                  flist.Add(fdir+'\'+searchrec.Name);
                if Uppercase(fType)=Uppercase(searchrec.Name) then
                  flist.Add(fdir+'\'+searchrec.Name);
              end;//是文件
        until (FindNext(SearchREC)<>0);//repeat
        result:=true;
      end;//if dDirectoryExists(fdir) then
    end;