I need to save and load captions of several labels. I tried use TMemo for this:
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;
But I don’t know how to set and get specific data of Label1, Label2, etc on the TMemo.
Based on this idea later I will use other components as TEdit.
I modified a little this code according to my requirements.
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;