如何将delphi保存到INI文件 [英] How to save to an INI file delphi
问题描述
我有一些按钮,当单击它们时,将其标题存储在TStringList中
I have buttons which when clicked store their caption in a TStringList
ChairList : TStringList;
procedure TForm1.FormCreate(Sender: TObject);
begin
ChairList := TStringList.Create;
end;
一个按钮的示例是:
procedure TForm1.Table17Click(Sender: TObject);
begin
Label1.Visible := false;
if (BottomPanel.Visible = false) then
begin
Label1.Visible := true;
LastClicked := 'Table 17';
ChairList.Add(LastClicked);
但是,当我读取文件时,没有任何结果.我通过使用 ShowMessage
进行了测试,只是看是否可以读取任何内容,而我只是得到一个空白的 ShowMessage
But when I read the file I get no result back. I tested it by using a ShowMessage
just to see if anything would be read and I just get a blank 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;
然后下一个过程是加载文件:
Then the next Procedure is the load file:
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
我如何制作它以保存标题,然后在我加载它时显示保存的任何表标题?我只需要它来保存所选对象的标题.当用户选择一个按钮时,它将标题添加到字符串列表,然后将其读取到ini文件
How can i make it so it saves the caption and then when i load it up it shows any table caption saved? I only need it to save the caption of an object selected. When the user selects a button it adds the caption to the stringlist which is then read to the ini file
推荐答案
尝试此代码!写入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;
读取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;
这篇关于如何将delphi保存到INI文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!