delphi如何保存到INI文件

not*_*oof 2 delphi ini file save

我有一些按钮,单击时将其标题存储在 TStringList 中

ChairList : TStringList;

procedure TForm1.FormCreate(Sender: TObject);
begin
ChairList := TStringList.Create;
end;
Run Code Online (Sandbox Code Playgroud)

单按钮的示例是:

procedure TForm1.Table17Click(Sender: TObject);
begin
Label1.Visible := false;
if (BottomPanel.Visible = false) then
begin
  Label1.Visible := true;

  LastClicked := 'Table 17';
  ChairList.Add(LastClicked);
Run Code Online (Sandbox Code Playgroud)

但是当我读取该文件时,我没有得到任何结果。我通过使用来测试它,ShowMessage只是为了看看是否会读取任何内容,但我只是得到一个空白ShowMessage

下面的代码是保存过程:

Procedure TForm1.SaveToFile(Const Filename : String);
Var
INI : TMemIniFile;

Procedure SaveReserve();
var
section : String;

Begin
  Section := 'Table';
  ini.writeString(Section, 'LastClickedID', LastClicked);
End;

begin
Ini := Tmeminifile.Create(filename);
ini.Clear;
try
SaveReserve();
Ini.UpdateFile;
finally
Ini.Free;
end;
end;
Run Code Online (Sandbox Code Playgroud)

然后下一个过程是加载文件:

Procedure TForm1.LoadFile(const Filename : String);
var
INI : TMemInifile;
Sections : TStringList;
i : Integer;
LastClickedID : String;
Procedure LoadChair(Const Section: String);
Begin
  LastClickedID := INI.ReadString(Section, 'LastClickedID', LastClicked)
End;

Begin
ChairList.Clear;

INI := TMEMINIFILE.Create(Filename);
 Try
 Sections := TStringList.Create;
 Try
   Ini.ReadSections(Sections);
   for i := 0 to (Sections.Count - 1) do
   Begin
     if startstext('LastClickedID', Sections[I]) then
      begin
        LastClickedID := (copy(Sections[I], 12, MaxInt));

      end;
   End;
 Finally
  Sections.Free;
 End;
 Finally
 INI.Free;
end;
ShowTable(LastClickedID);
end;

ShowTable(LastClickedID); is just the part where i test it
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到它保存标题,然后当我加载它时它显示保存的任何表格标题?我只需要它来保存所选对象的标题。当用户选择一个按钮时,它会将标题添加到字符串列表中,然后将其读取到 ini 文件中

小智 5

试试这个代码! 写入ini的流程:

procedure write_ini;
var
  ini:TIniFile;
begin
  ini:=TIniFile.Create('MainForm.ini');
  ini.WriteInteger('FORM', 'Top', MainForm.Top);
  ini.WriteInteger('FORM', 'Left', MainForm.Left);
  ini.WriteInteger('FORM', 'Width', MainForm.Width);
  ini.WriteInteger('FORM', 'Height', MainForm.Height);
  ini.WriteString('USER', 'Name', MainForm.userName.Text);
  ini.WriteInteger('USER','Pol', MainForm.Combo.ItemIndex);
  ini.Free;
end;
Run Code Online (Sandbox Code Playgroud)

读取ini的过程:

 procedure read_ini;
    var
     ini:TIniFile;
    begin
      ini:=TiniFile.Create('MainForm.ini');
      MainForm.Top:=ini.ReadInteger('FORM', 'Top', 0);
      MainForm.Left:=ini.ReadInteger('FORM', 'Left', 0);
      MainForm.Width:=ini.ReadInteger('FORM', 'Width', 226);
      MainForm.Height:=ini.ReadInteger('FORM', 'Height', 123);
      MainForm.userName.Text:=ini.ReadString('USER', 'Name', 'Anonim');
      MainForm.Combo.ItemIndex:=ini.ReadInteger('USER', 'Pol', 1);
      ini.Free;
    end;
Run Code Online (Sandbox Code Playgroud)