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)

for i := 0 to 29 do
  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.

Recommended Answers

All 3 Replies

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 IS TEdit
THEN
Begin
T := Components as TEdit;
A[Components.Tag] := T.NAme + ' is ' + T.Text;
End;

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

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

Hope that helps

P.

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)

for i := 0 to 29 do
  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.

Supposing the 30 TEdit are in a TPanel called Panel1, I would do it like this:

for i:= 0 to 29 do
  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:

for i:= 0 to 29 do begin
  MyEdit:= TEdit(Panel1.FindChildControl('E'+IntToStr(i)));
  if Assigned(MyEdit) then
    MyEdit.Text:= a[i];
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.

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

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.