制作的回答只是为了显示正确格式化形式的代码示例
阅读:
所以,你的代码将成为像
const ini_def_sect = 'Default Section';
procedure TForm1.Button3Click(Sender: TObject);
var
F: TCustomIniFile;
begin
SaveDialog1.Filter := 'GPP files (*.GPP)|*.GPP';
if SaveDialog1.Execute then
begin
F := TIniFile.Create(SaveDialog1.Filename);
try
F.WriteString(ini_def_sect, label7.Name, label7.Caption);
F.WriteString(ini_def_sect, label21.Name, label21.Caption);
F.WriteBool(ini_def_sect, radiobutton1.Name, radiobutton1.Checked);
F.UpdateFile;
finally
F.Destroy;
end;
end;
end;
或者在VCL与基于风格(很多人不喜欢,他们不喜欢帕斯卡尔with
语句)
const ini_def_sect = 'Default Section';
procedure TForm1.Button3Click(Sender: TObject);
begin
SaveDialog1.Filter := 'GPP files (*.GPP)|*.GPP';
if SaveDialog1.Execute then
begin
with TIniFile.Create(Savedialog1.Filename) do
try
WriteString(ini_def_sect, label7.Name, label7.Caption);
WriteString(ini_def_sect, label21.Name, label21.Caption);
WriteBool(ini_def_sect, radiobutton1.Name, radiobutton1.Checked);
UpdateFile;
finally
Destroy;
end;
end;
end;
而且你真的真的应该给你的变量(包括标签,表单,单选按钮等)合理的名称,而你仍然可以记住一些有关这些都意味着什么。相信我 - 一个月或两个月,你会忘记。
PS:读取结构文件就应该像这样
procedure TMainForm.btnOpenClick(Sender: TObject);
begin
OpenDialog1.Filter := 'GPP files (*.GPP)|*.GPP';
if OpenDialog1.Execute then
begin
with TIniFile.Create(OpenDialog1.Filename) do
try
label7.Caption := ReadString(ini_def_sect, label7.Name, '');
label21.Caption := ReadString(ini_def_sect, label21.Name, '');
radiobutton1.Checked := ReadBool(ini_def_sect, radiobutton1.Name, False);
finally
Destroy;
end;
end;
end;
你有更大的问题。就像你没有命名你的控件一样。事实上,你不使用try/finally。您正在使用非结构化文件格式的事实。 –
我必须保存单选按钮的状态,我的意思是如果选中或取消选中...也许有另一种简单的方法来保存我的日期在外部文件? –
使用ini文件或xml文件来保存状态。 –