Delphi and Kylix

Moderators: pritaeas
Number of threads: 7244
Number of posts: 19051

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Trying to neatly add a range of edit fields Posted by LordBug on 16 May 2006 at 7:35 PM
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:
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;


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

Cheers in advance
Report
Re: Trying to neatly add a range of edit fields Posted by zibadian on 16 May 2006 at 8:38 PM
: 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:
:
: 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;

:
: 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.
Report
Re: Trying to neatly add a range of edit fields Posted by LordBug on 16 May 2006 at 8:51 PM
Thanks yet again zibadian, that put me on the right path :)



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.