如何保存和加载多个标签的标题?
How to save and load captions of several labels?
我需要保存和加载多个标签的标题。我尝试为此使用 TMemo:
procedure TForm1.Button1Click(Sender: TObject);
begin
if SaveDialog1.Execute then
begin
Memo1.Lines.SaveToFile(saveDialog1.FileName+'.txt');
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
if OpenDialog1.Execute then
begin
Memo1.Lines.LoadFromFile(OpenDialog1.FileName);
end;
end;
但不知道如何在TMemo上设置和获取Label1、Label2等具体数据
基于这个想法后面我会使用其他组件作为TEdit
在这里搞个备忘录真的没什么意义。您可能正在寻找 TStringList
.
需要先将标签内容写入字符串列表:
procedure TForm1.Button1Click(Sender: TObject);
var
SL: TStringList;
begin
if SaveDialog1.Execute then
begin
SL := TStringList.Create;
try
SL.Add(Edit1.Text);
SL.Add(Edit2.Text);
// Rest of edits
SL.SaveToFile(SaveDialog1.FileName);
finally
SL.Free;
end;
end;
end;
回读:
procedure TForm1.Button2Click(Sender: TObject);
var
SL: TStringList;
begin
if OpenDialog1.Execute then
begin
SL := TStringList.Create;
try
SL.LoadFromFile(OpenDialog1.FileName);
Edit1.Text := SL[0];
Edit2.Text := SL[1];
// Same for rest of edits
finally
SL.Free;
end;
end;
end;
如果您必须对大量编辑控件执行此操作,则您可能使用了错误的用户界面。虽然不知道您要完成什么,但很难提出其他建议。
我认为最简单的方法是使用字符串列表 (TStringList
) 而不是备忘录控件。它允许您保存 name/value 对,因此名称可以是标签的名称(或任何其他唯一标识符),它们的值是要在标签中显示的标题。
procedure TForm1.LoadFile(AFilename: String);
var
Lst: TStringList;
begin
Lst:= TStringList.Create;
try
Lst.LoadFromFile(AFilename);
Label1.Caption:= Lst.Values['Label1'];
Label2.Caption:= Lst.Values['Label2'];
...
finally
Lst.Free;
end;
end;
procedure TForm1.SaveFile(AFilename: String);
var
Lst: TStringList;
begin
Lst:= TStringList.Create;
try
Lst.Values['Label1']:= Label1.Caption;
Lst.Values['Label2']:= Label2.Caption;
...
Lst.SaveToFile(AFilename);
finally
Lst.Free;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
if SaveDialog1.Execute then begin
SaveFile(saveDialog1.FileName+'.txt');
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
if OpenDialog1.Execute then begin
LoadFile(OpenDialog1.FileName);
end;
end;
这个code我根据自己的要求做了一点修改
procedure TForm1.ButtonSaveClick(Sender: TObject);
var
I: Integer;
Strings: TStringList;
begin
if SaveDialog1.Execute then
begin
Strings := TStringList.Create;
try
for I := 0 to ComponentCount - 1 do
begin
if Components[I] is TLabel then
Strings.Values[Components[I].Name] := TLabel(Components[I]).Caption;
if Components[I] is TEdit then
Strings.Values[Components[I].Name] := TEdit(Components[I]).Text;
end;
Strings.SaveToFile(SaveDialog1.FileName);
finally
Strings.Free;
end;
end;
end;
procedure TForm1.ButtonLoadClick(Sender: TObject);
var
I: Integer;
Control: TComponent;
Strings: TStringList;
begin
if OpenDialog1.Execute then
begin
Strings := TStringList.Create;
try
Strings.LoadFromFile(OpenDialog1.FileName);
for I := 0 to Strings.Count - 1 do
begin
Control := FindComponent(Strings.Names[I]);
if Control is TLabel then
TLabel(Control).Caption := Strings.ValueFromIndex[I];
if Control is TEdit then
TEdit(Control).Text := Strings.ValueFromIndex[I];
end;
finally
Strings.Free;
end;
end;
end;
我需要保存和加载多个标签的标题。我尝试为此使用 TMemo:
procedure TForm1.Button1Click(Sender: TObject);
begin
if SaveDialog1.Execute then
begin
Memo1.Lines.SaveToFile(saveDialog1.FileName+'.txt');
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
if OpenDialog1.Execute then
begin
Memo1.Lines.LoadFromFile(OpenDialog1.FileName);
end;
end;
但不知道如何在TMemo上设置和获取Label1、Label2等具体数据
基于这个想法后面我会使用其他组件作为TEdit
在这里搞个备忘录真的没什么意义。您可能正在寻找 TStringList
.
需要先将标签内容写入字符串列表:
procedure TForm1.Button1Click(Sender: TObject);
var
SL: TStringList;
begin
if SaveDialog1.Execute then
begin
SL := TStringList.Create;
try
SL.Add(Edit1.Text);
SL.Add(Edit2.Text);
// Rest of edits
SL.SaveToFile(SaveDialog1.FileName);
finally
SL.Free;
end;
end;
end;
回读:
procedure TForm1.Button2Click(Sender: TObject);
var
SL: TStringList;
begin
if OpenDialog1.Execute then
begin
SL := TStringList.Create;
try
SL.LoadFromFile(OpenDialog1.FileName);
Edit1.Text := SL[0];
Edit2.Text := SL[1];
// Same for rest of edits
finally
SL.Free;
end;
end;
end;
如果您必须对大量编辑控件执行此操作,则您可能使用了错误的用户界面。虽然不知道您要完成什么,但很难提出其他建议。
我认为最简单的方法是使用字符串列表 (TStringList
) 而不是备忘录控件。它允许您保存 name/value 对,因此名称可以是标签的名称(或任何其他唯一标识符),它们的值是要在标签中显示的标题。
procedure TForm1.LoadFile(AFilename: String);
var
Lst: TStringList;
begin
Lst:= TStringList.Create;
try
Lst.LoadFromFile(AFilename);
Label1.Caption:= Lst.Values['Label1'];
Label2.Caption:= Lst.Values['Label2'];
...
finally
Lst.Free;
end;
end;
procedure TForm1.SaveFile(AFilename: String);
var
Lst: TStringList;
begin
Lst:= TStringList.Create;
try
Lst.Values['Label1']:= Label1.Caption;
Lst.Values['Label2']:= Label2.Caption;
...
Lst.SaveToFile(AFilename);
finally
Lst.Free;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
if SaveDialog1.Execute then begin
SaveFile(saveDialog1.FileName+'.txt');
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
if OpenDialog1.Execute then begin
LoadFile(OpenDialog1.FileName);
end;
end;
这个code我根据自己的要求做了一点修改
procedure TForm1.ButtonSaveClick(Sender: TObject);
var
I: Integer;
Strings: TStringList;
begin
if SaveDialog1.Execute then
begin
Strings := TStringList.Create;
try
for I := 0 to ComponentCount - 1 do
begin
if Components[I] is TLabel then
Strings.Values[Components[I].Name] := TLabel(Components[I]).Caption;
if Components[I] is TEdit then
Strings.Values[Components[I].Name] := TEdit(Components[I]).Text;
end;
Strings.SaveToFile(SaveDialog1.FileName);
finally
Strings.Free;
end;
end;
end;
procedure TForm1.ButtonLoadClick(Sender: TObject);
var
I: Integer;
Control: TComponent;
Strings: TStringList;
begin
if OpenDialog1.Execute then
begin
Strings := TStringList.Create;
try
Strings.LoadFromFile(OpenDialog1.FileName);
for I := 0 to Strings.Count - 1 do
begin
Control := FindComponent(Strings.Names[I]);
if Control is TLabel then
TLabel(Control).Caption := Strings.ValueFromIndex[I];
if Control is TEdit then
TEdit(Control).Text := Strings.ValueFromIndex[I];
end;
finally
Strings.Free;
end;
end;
end;