[Delphi 3] using a var in object name.. "edit{i}"

Please support our Pascal and Delphi advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Nov 2009
Posts: 2
Reputation: laki4546 is an unknown quantity at this point 
Solved Threads: 0
laki4546 laki4546 is offline Offline
Newbie Poster

[Delphi 3] using a var in object name.. "edit{i}"

 
0
  #1
31 Days Ago
Hello

I am using Delphi 3.

I have 30 edit fields named e0, e1, e2, ..., e29. The thing is, that I want to put something into these edit fields, to be specific: I want to put variable a[0] into e0, a[1] into e1 and so on. i could of course write the same thing 30 times, but that takes time and is probably not the best way to do it.

So what I need is a way to call object names with variables. For example: (some pseudo-code)

Pascal and Delphi Syntax (Toggle Plain Text)
  1. for i := 0 to 29 do
  2. e{i} := a[i];

Of course this will not work this way, but I think you can see what I am looking for now.

Is there any way to do this? It's not the first time it could come handy, but this time I really need it.





small offtopic question: is there any open source delphi sdk/ide? i'm kinda tired of delphi 3.
Last edited by laki4546; 31 Days Ago at 3:26 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 10
Reputation: quaifp1 is an unknown quantity at this point 
Solved Threads: 3
quaifp1 quaifp1 is offline Offline
Newbie Poster
 
0
  #2
30 Days Ago
Hi,

Personally i would be tempted to use the tag field in tEdit to enumerate each field.

With a form and 5 TEdits [Edit1..5] with tags 0..4
An Array A[0..4] of String
And a button

Put say a,b,c,d,e into your 5 Tedits and hit the button

procedure TForm1.Button1Click(Sender: TObject);

VAR
A : Array[0..4] OF String;
I : Integer;
T : TEdit;
begin
For i := 0 to ComponentCount - 1 DO
IF Components[i] IS TEdit
THEN
Begin
T := Components[i] as TEdit;
A[Components[i].Tag] := T.NAme + ' is ' + T.Text;
End;

For I := 0 to 4 DO
MessageDlg(A[i],MtInformation,[MbOK],0);
end;

Will result in 5 mes boxes: Edit1 is a, edit2 is b etc.

Hope that helps

P.

Originally Posted by laki4546 View Post
Hello

I am using Delphi 3.

I have 30 edit fields named e0, e1, e2, ..., e29. The thing is, that I want to put something into these edit fields, to be specific: I want to put variable a[0] into e0, a[1] into e1 and so on. i could of course write the same thing 30 times, but that takes time and is probably not the best way to do it.

So what I need is a way to call object names with variables. For example: (some pseudo-code)

Pascal and Delphi Syntax (Toggle Plain Text)
  1. for i := 0 to 29 do
  2. e{i} := a[i];

Of course this will not work this way, but I think you can see what I am looking for now.

Is there any way to do this? It's not the first time it could come handy, but this time I really need it.





small offtopic question: is there any open source delphi sdk/ide? i'm kinda tired of delphi 3.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 40
Reputation: BitFarmer is an unknown quantity at this point 
Solved Threads: 3
BitFarmer BitFarmer is offline Offline
Light Poster
 
0
  #3
22 Days Ago
Supposing the 30 TEdit are in a TPanel called Panel1, I would do it like this:

Pascal and Delphi Syntax (Toggle Plain Text)
  1. for i:= 0 to 29 do
  2. TEdit(Panel1.FindChildControl('E'+IntToStr(i))).Text:= a[i];

This is the most similar to your example it can get, but, I would do it a little more robust:

Pascal and Delphi Syntax (Toggle Plain Text)
  1. for i:= 0 to 29 do begin
  2. MyEdit:= TEdit(Panel1.FindChildControl('E'+IntToStr(i)));
  3. if Assigned(MyEdit) then
  4. MyEdit.Text:= a[i];
  5. end;

About a free pascal IDE+compiler, you have FreePascal + Lazarus, very delphi like and do compile for mac, linux, windows... even iPhone I read today!

The only bad things compared to delphi is that it takes much more time to compile (delphi is king here) and the exe file size is also much bigger.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 2
Reputation: laki4546 is an unknown quantity at this point 
Solved Threads: 0
laki4546 laki4546 is offline Offline
Newbie Poster
 
0
  #4
20 Days Ago
Thanks quaifp1 and BitFarmer for the solution!
I'm sorry for answering so late, but I have so much to do and are almost never at home in the week so I don't find much time to do other things.
Haven't tried it yet, but I will come back and ask if there were any problems. (although the structure looks relatively simple )

As for Lazarus, I will try it, thanks for the tip. =D
Reply With Quote Quick reply to this message  
Reply

Tags
delphi

Message:



Other Threads in the Pascal and Delphi Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC