Hi everyone,
I have the following code to save the text inside an editbox:

var
  Data: TStringList;
begin
  Data := TStringList.Create;
  try
    Data.Append(Edit1.Text);
    Data.SaveToFile(Savedialog1.Filename +'.txt');
  finally
    Data.Free;
  end;
end;

And this code to load it to that editbox where i saved it from:

var
  Data: TStringList;
begin
  Data := TStringList.Create;
  try
    Data.LoadFromFile(OpenDialog1.Filename);
    if Data.Count > 0 then
      Edit1.Text := Data.Strings[0];
  finally
    Data.Free;
  end;
end;

My Question is:

Is it possible to save like...10 editboxes and load them into those 10 again?

Recommended Answers

All 4 Replies

Yes. Easy.

Save:

procedure TForm1.SaveButtonClick(Sender: TObject);
var
  i: integer;
begin
  with TStringList.Create do
  try
    for i := 0 to self.ControlCount-1 do
      if (self.Controls[i] is TEdit) then
          Add(self.Controls[i].Name+'='+(self.Controls[i] as TEdit).Text);
    SaveToFile('Filename.txt');
  finally
    Free;
  end;
end;

load:

procedure TForm1.LoadButtonClick(Sender: TObject);
var
  i,idx: integer;
begin
  with TStringList.Create do
  try
    LoadFromFile('Filename.txt');
    for i := 0 to self.ControlCount-1 do
    begin
      idx := IndexOfName(self.Controls[i].Name);
      if idx <> -1 then
         (self.Controls[i] as TEdit).Text := ValueFromIndex[idx];
    end;
  finally
    Free;
  end;
end;

Great it works like a charm, but i have another question,
I expanded my program with 4 memo fields but i don't know where to place thew code to save and load the memo fields. So far i used them everywhere but no result, this is the latest update, i place them here:

var
  i: integer;
begin
  with TStringList.Create do
    try
    if SaveDialog1.Execute then
      for i := 0 to self.ControlCount-1 do
        if (self.Controls[i] is TEdit) then
          Add(self.Controls[i].Name+'='+(self.Controls[i] as TEdit).Text);
          SaveToFile(SaveDialog1.Filename);
        finally
          Free;
        end;
        Memo1.Lines.SaveToFile(SaveDialog1.FileName); <----
        Memo2.Lines.SaveToFile(SaveDialog1.FileName); <----
        Memo3.Lines.SaveToFile(SaveDialog1.FileName); <----
        Memo4.Lines.SaveToFile(SaveDialog1.FileName); <----
      end;

and for load:

var
  i,idx: integer;
begin
  with TStringList.Create do
    try
    if OpenDialog1.Execute then
    begin
      LoadFromFile(OpenDialog1.Filename);
      Memo1.Lines.LoadFromFile(OpenDialog1.FileName); <----
      Memo2.Lines.LoadFromFile(OpenDialog1.FileName); <----
      Memo3.Lines.LoadFromFile(OpenDialog1.FileName); <----
      Memo4.Lines.LoadFromFile(OpenDialog1.FileName); <----
      end;
        for i := 0 to self.ControlCount-1 do
        begin
          idx := IndexOfName(self.Controls[i].Name);
            if idx <> -1 then
            (self.Controls[i] as TEdit).Text := ValueFromIndex[idx];
          end;
        finally
          Free;
        end;
      end;

But it only loads the memo fields, or only saves the memofields?

Then it is better to use INI files. But this is not the best option.

var
  i,j: integer;
begin
  with TIniFile.Create(ExtractFilePath(ParamStr(0))+'Filename.txt') do
  try
    for i := 0 to self.ControlCount-1 do
      if (self.Controls[i] is TEdit) then
          WriteString(self.Controls[i].Name,'Value',(self.Controls[i] as TEdit).Text)
      else
      if (self.Controls[i] is TMemo) then
         for j := 0 to (self.Controls[i] as TMemo).Lines.Count - 1 do
           WriteString(self.Controls[i].Name,'Line'+intToStr(j),(self.Controls[i] as TMemo).Lines[j]);
  finally
    Free;
  end;
var
  i,j,idx: integer;
  StrList: TStringList;
begin
  with TIniFile.Create(ExtractFilePath(ParamStr(0))+'Filename.txt') do
  try
    for i := 0 to self.ControlCount-1 do
     if (self.Controls[i] is TEdit) then
        (self.Controls[i] as TEdit).Text := ReadString(self.Controls[i].Name,'Value','')
     else
     if (self.Controls[i] is TMemo) then
     begin
       StrList := TStringList.Create;
       try
         ReadSectionValues(self.Controls[i].Name,StrList);
         (self.Controls[i] as TMemo).Clear;
         for j := 0 to StrList.Count - 1 do
           (self.Controls[i] as TMemo).Lines.Add(StrList.ValueFromIndex[j]);
       finally
         StrList.Free;
       end;
     end;
  finally
    Free;
  end;

Great it works perfect. Thanks alot

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.