1.如何在listbox中显示程序所在目录的文件名,即szFilePath需要赋值为相对目录名
2.如何只显示文件名,而不显示后缀.txtprocedure TForm1.FormShow(Sender: TObject);
var
SearchRec: TSearchRec;
nResult: Integer;
const
szFilePath = 'd:\*.txt';
begin
//填充ListBox
ListBox1.Items.clear;
nResult:=FindFirst(szFilePath,faAnyFile,SearchRec); //查找第一个文件
while nResult = 0 do //如果返回值为0表示找到文件
begin
//将找到的文件添加到ListBox中;ExtractFilepath函数用于提取文件路径
ListBox1.Items.Add(SearchRec.Name);
nResult:=FindNext(SearchRec); //继续查找下一个文件,至到返回值不为0时
end;
end;

解决方案 »

  1.   

    1。path=ExtractFilePath(Application.ExeName));得到当前应用程序所在的路径
    2。将 ListBox1.Items.Add(SearchRec.Name);改为
    ListBox1.Items.Add(Copy(SearchRec.Name,1,Length(SearchRec.Name)-POS('.',SearchRec.Name)+1);
      

  2.   

    var
      Form1: TForm1;
      i:integer = 1;
      sss: Array [1..10] of string;
    implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
    var
     SearchRec:tSearchRec;
     FindResult:integer;
    begin
    FindResult := FindFirst('C:\top\'+ '*.txt',FaAnyFile,SearchRec);
    while findresult=0 do
    begin
       listbox1.items.Add(ChangeFileExt(SearchRec.Name,''));
       sss[i]:='C:\top\'+SearchRec.Name;
       i:=i+1;
       FindResult :=findnext(Searchrec);
    end;
    end;procedure TForm1.ListBox1DblClick(Sender: TObject);
    begin
    if listbox1.Selected[listbox1.ItemIndex]=true then
            begin
            edit1.Text:=sss[listbox1.ItemIndex+1];
            end
            else
            Exit;
    end;
      

  3.   

    我这样来的得到当前目录下的.txt文件名并显示于listbox中,为什么不行呢?unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        ListBox1: TListBox;
        procedure FormShow(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.FormShow(Sender: TObject);
    var
    SearchRec: TSearchRec;
    nResult: Integer;
    const
    szFilePath =Application.EXEName+'\*.txt';
    begin
    //填充ListBox
    ListBox1.Items.clear;
    nResult:=FindFirst(szFilePath,faAnyFile,SearchRec); //查找第一个文件
    while nResult = 0 do //如果返回值为0表示找到文件
    begin
    //将找到的文件添加到ListBox中;ExtractFilepath函数用于提取文件路径
    ListBox1.Items.Add(Copy(SearchRec.Name,1,Length(SearchRec.Name)-POS('.',SearchRec.Name)+1));
    nResult:=FindNext(SearchRec); //继续查找下一个文件,至到返回值不为0时
    end;
    end;end.
      

  4.   

    procedure TForm1.FormShow(Sender: TObject);
    var
    SearchRec: TSearchRec;
    s:string;
    nResult: Integer;
    //const
    //szFilePath =ExtractFilePath(Application.ExeName));begin
    //填充ListBox
    s:=ExtractFilePath(Application.ExeName)+'\*.txt';
    ListBox1.Items.clear;
    nResult:=FindFirst(s,faAnyFile,SearchRec); //查找第一个文件
    while nResult = 0 do //如果返回值为0表示找到文件
    begin
    //将找到的文件添加到ListBox中;ExtractFilepath函数用于提取文件路径
    ListBox1.Items.Add(Copy(SearchRec.Name,1,Length(SearchRec.Name)-4));
    nResult:=FindNext(SearchRec); //继续查找下一个文件,至到返回值不为0时
    end;
    end;
      

  5.   

    以上是第二问的,第一问是function tform1.showfile(s:string):boolean;
    var
    SearchRec: TSearchRec;
    ss:string;
    nResult: Integer;
    //const
    //szFilePath =ExtractFilePath(Application.ExeName));begin
    //填充ListBox
    ss:=s+'\*.txt';
    ListBox1.Items.clear;
    nResult:=FindFirst(ss,faAnyFile,SearchRec); //查找第一个文件
    while nResult = 0 do //如果返回值为0表示找到文件
    begin
    //将找到的文件添加到ListBox中;ExtractFilepath函数用于提取文件路径
    ListBox1.Items.Add(Copy(SearchRec.Name,1,Length(SearchRec.Name)-4));
    nResult:=FindNext(SearchRec); //继续查找下一个文件,至到返回值不为0时
    end;
    end;凋用:
    self.showfile('d:\aa');