-1

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.

Goerman
  • 163
  • 7
  • 18

3 Answers3

4

I believe the easiest way is to use a string list (TStringList) instead of a memo control. It allows you to save name/value pairs, so the names could be the names of your labels (or any other unique identifier), and their values are the captions to show in the labels.

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;
Jerry Dodge
  • 26,858
  • 31
  • 155
  • 327
3

It doesn't really make any sense to have a memo involved here. You're probably looking for a TStringList.

You need to write the label contents to the string list first:

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;

To read back:

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;

If you're having to do this for a lot of edit controls, you're probably using the wrong user interface. Without knowing what you're trying to accomplish, it's hard to suggest something else though.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
Ken White
  • 123,280
  • 14
  • 225
  • 444
  • I personally would never encourage someone to copy values from labels to a memo control then to a file, and vice-versa. – Jerry Dodge Apr 08 '15 at 03:01
  • @Jerry: I agree. Thought about the question (was watching something on TV) and was updating the answer when you commented. – Ken White Apr 08 '15 at 03:02
0

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;
Goerman
  • 163
  • 7
  • 18