如何读取一个文本文件,并将该文件的内容每一行添加到一个Listbox中,
之后我可以对文本内容进行添加、删除、修改操作,具体在Listbox中动态实现,并且将更新内容写入到文本文件中?请问如何实现?
请教高手帮忙!!

解决方案 »

  1.   

    //Read文本文件
    var
      txtFile: TextFile;
      txtString: String;
    begin
      AssignFile(txtFile,'C:\test.txt');
      Reset(txtFile);
      try
        while not Eof(txtFile) do
        begin
          Readln(txtFile,txtString);
          ListBox.Items.Add(txtString);  //忘记啦!好像是这么来着!
        end;
      finally
        CloseFile(txtFile);
      end;
    end;
      

  2.   

    //Write文本文件
    var
      i: Integer;
      txtFile: TextFile;
      txtString: String;
    begin
      AssignFile(txtFile,'C:\test.txt');
      Rewrite(txtFile);
      try
        for i:=0 to ListBox.Items.Count - 1 do
        begin
          Writeln(txtFile,ListBox.Items[i].Text);  //忘记啦!好像是这么来着!
        end;
      finally
        CloseFile(txtFile);
      end;
    end;
      

  3.   

    ComboBox1.Items.SaveToFile(ExtractFilePath(Application.ExeName)+'file1.txt');
    (内容写入到文本文件中)
      

  4.   

    读取文件
    ListBox.Items.LoadFromFile('');无重复信息
    把ListBox.Sorted设为True;
    检查相邻行是否相同,如果相同,删除之写文件
    ListBox.Items.SaveToFile('');
      

  5.   

    用CreateFile创建文件
    再用ReadFile读取文件内容
    WriteFile写数据到文件//ReadFile.....for i:=0 to Count-1 do
      begin
        if str:string<>ListBox1.Items.String[i] then
          Listbox1.Items.Add(str:String);
      end;ListBox1.DeleteSelected;  // 删除所选择的内容for i:=0 to Count-1 do
      begin
        if ListBox1.Items.String[i]=str:String then
          ListBox1.Selected[i]:=True;
      end;
    ListBox1.DeleteSelected;for i:=0 to Count-1 do
      begin
        if (ListBox1.Selected[i]) and (InputQuery('提示','请输入新数据',str:String)) and (str:String<>'') then
          ListBox1.Items.ExChange(i,ListBox1.Add(str:String));
      end;
    ListBox1.Items.Delete[ListBox1.Count];ListBox1.SaveToFile(FileName:String);//WriteFile....
      

  6.   

    读取文件:
    procedure LoadFile(AListBox : TListBox; AFileName : String);
    var
      sl : TStringList;
      i : Integer;
    begin
      sl := TStringList.Create;
      try
        sl.LoadFromFile(AFileName);
        for i := 0 to sl.Count - 1 do
          if AListBox.Items.IndexOf(sl[i]) < 0 then
            AListBox.Items.Add(sl[i]);
      finally
        sl.Free;
      end;
    end;至于存盘直接用AListBox.Items.SaveToFile('c:\xxx\xxx.xxx');就行了