Trying to neatly add a range of edit fields - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Trying to neatly add a range of edit fields

LordBugLordBug Posts: 12Member
Hi

I've got about 27 edit boxes on a form, which should result in a total.
Can someone help me create a neat little bit of code to do this (As opposed to something horrible like "i := StrToInt(Edit1.Text) + StrToInt(Edit2.Text) +".

I've tried this little code:
[code]
procedure TForm1.Button1Click(Sender: TObject);
var
i,ii,si : integer;
s : String;
begin
ii := 0;
for i := 1 to 27 do
begin
s := 'Edit'+IntToStr(i)+'.Text';
si := StrToInt(s);
ii := ii + si;
end;
Listbox1.Items.Add(IntToStr(ii));

end;[/code]

But whenever I run that code, it gives me an error that says
'Edit1.Text' is not a valid integer value.

Cheers in advance

Comments

  • zibadianzibadian Posts: 6,349Member
    : Hi
    :
    : I've got about 27 edit boxes on a form, which should result in a total.
    : Can someone help me create a neat little bit of code to do this (As opposed to something horrible like "i := StrToInt(Edit1.Text) + StrToInt(Edit2.Text) +".
    :
    : I've tried this little code:
    : [code]
    : procedure TForm1.Button1Click(Sender: TObject);
    : var
    : i,ii,si : integer;
    : s : String;
    : begin
    : ii := 0;
    : for i := 1 to 27 do
    : begin
    : s := 'Edit'+IntToStr(i)+'.Text';
    : si := StrToInt(s);
    : ii := ii + si;
    : end;
    : Listbox1.Items.Add(IntToStr(ii));
    :
    : end;[/code]
    :
    : But whenever I run that code, it gives me an error that says
    : 'Edit1.Text' is not a valid integer value.
    :
    : Cheers in advance
    :
    In your code the value of s is not code, but a normal string.
    You should use the FindComponent() method to get the component reference. Then type-cast the component to an TEdit, and then you can access the Text property.
  • LordBugLordBug Posts: 12Member
    Thanks yet again zibadian, that put me on the right path :)
Sign In or Register to comment.