Looping through all TEdit controls on form

I have a form that needs the contents of all the Tedit controls saved to an ini file each time it's closed and filled each time it's opened.  I was doing this line by line for each control. Since the app has gotten larger and may continue to grow, this method gets tedius.  For some reason it's seizing when reading the string in.

So here's what I have:

procedure TForm1.FormCreate(Sender: TObject);
var
IniName:String; Ini:TIniFile;
i: integer;
Temp: TEdit;

begin

IniName:=application.exename;
IniName:=copy(IniName,1,length(IniName)-3)+'ini';
Ini:=tinifile.create(IniName);

  try
          for i := 0 to ComponentCount - 1 do
          begin
                if  (Components[i] is TEdit) then
                Temp.Name := Components[i].Name;
                ini.ReadString(Temp.Name,'Text',Temp.Text);
          end;
  finally

        Ini.Free;
   end;

   end;
LVL 1
digitalwavIT Infrastructure ManagerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike LittlewoodEngineerCommented:
Why not just use a stringlist instead.
You might need to change the filename as it needs the full path to where you are saving.

procedure TForm1.FormCreate(Sender: TObject);
var
  IniName:String;
  i: integer;
  Temp: TEdit;
  slTemp: TStringList;
begin

  IniName:=application.exename;
  IniName:=copy(IniName,1,length(IniName)-3)+'.ini';

  try
    slTemp := TStringList.Create;
    for i := 0 to ComponentCount - 1 do
    begin
      if  (Components[i] is TEdit) then
        slTemp.Add( TEdit(Components[i]).Text );
    end;
    slTemp.SaveToFile(IniName);
  finally
    FreeAndNil(slTemp)
  end;
end;
sas13Commented:
//read
for i := 0 to ComponentCount - 1 do
 if  (Components[i] is TEdit) then begin
   Temp := TEdit(Components[i]);
    ini.ReadString(Temp.Name,'Text',Temp.Text);
 end;

// write
for i := 0 to ComponentCount - 1 do
 if  (Components[i] is TEdit) then begin
   Temp := TEdit(Components[i]);
    ini.WriteString(Temp.Name,'Text',Temp.Text);
 end;

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mike LittlewoodEngineerCommented:
Doh, you are reading back ... same thing but just use load from file :)
Maybe this will do, though u will need to make sure you are writing the correct data to the correct edit box

procedure TForm1.FormCreate(Sender: TObject);
var
  IniName:String;
  i: integer;
  Temp: TEdit;
  slTemp: TStringList;
begin

  IniName:=application.exename;
  IniName:=copy(IniName,1,length(IniName)-3)+'.ini';

  try
    slTemp := TStringList.Create;
    slTemp.LoadFromFile(IniName);
    for i := 0 to ComponentCount - 1 do
    begin
      if  (Components[i] is TEdit) then
        TEdit(Components[i]).Text := slTemp.Strings[i];
    end;
  finally
    FreeAndNil(slTemp)
  end;
end;
sas13Commented:
and more: in RX lib exists component with full source - TFormStorage.
digitalwavIT Infrastructure ManagerAuthor Commented:
Perfect!  Thanks for everone's help!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.