Discussion:
array of TEdit as a constant
(too old to reply)
Harald Knautzberg
2005-01-14 22:17:54 UTC
Permalink
Hello Newsgroup,

I need an array of TEdit declared as a constant but delphi said: error -
undefined designator ed_key1, ed_key2, ed_key3 - constant expression
expected (hope that the translation fits :-)

here's my code:

[code]
unit u_frm_rotoren;

interface
...
Forms,
Dialogs, StdCtrls, Buttons, u_typen;

type
Tfrm_settings = class(TForm)
ed_key1: TEdit;
ed_key2: TEdit;
ed_key3: TEdit;
...

implementation

const ArrEdits : array [0..2] of TEdit
= (ed_key1, ed_key2, ed_key3);
[/code]

Thanks for any ideas,
Harry
Bruce Roberts
2005-01-14 22:37:10 UTC
Permalink
Post by Harald Knautzberg
I need an array of TEdit declared as a constant but delphi said: error -
undefined designator ed_key1, ed_key2, ed_key3 - constant expression
expected (hope that the translation fits :-)
You can't do it. I suggest that you reexamine your design. You might find
that there is a simpler, more Delphi solution that doesn't require the
array. Tell us what you are trying to do, someone can probably help.
Harald Knautzberg
2005-01-14 23:19:27 UTC
Permalink
Post by Bruce Roberts
You can't do it. I suggest that you reexamine your design. You might find
that there is a simpler, more Delphi solution that doesn't require the
array. Tell us what you are trying to do, someone can probably help.
I have round about 40 edit fields where I have to set the text property.
The standard value for the text property of each edit came from an array
of char. Now, I'm not very happy about init the edits by direct
assigning like:

edit1.text := myarray[1];
...
edit40.text := myarray[4];

My idea was to do it in a way like this:

for i := 1 to 40 do
editarray[i] := myarray[i];

I also have to write back the values (on change) in the other array.

thx and ragards,
Harry
J French
2005-01-15 06:31:34 UTC
Permalink
On Sat, 15 Jan 2005 00:19:27 +0100, Harald Knautzberg
Post by Harald Knautzberg
Post by Bruce Roberts
You can't do it. I suggest that you reexamine your design. You might find
that there is a simpler, more Delphi solution that doesn't require the
array. Tell us what you are trying to do, someone can probably help.
I have round about 40 edit fields where I have to set the text property.
The standard value for the text property of each edit came from an array
of char. Now, I'm not very happy about init the edits by direct
edit1.text := myarray[1];
...
edit40.text := myarray[4];
for i := 1 to 40 do
editarray[i] := myarray[i];
I also have to write back the values (on change) in the other array.
thx and ragards,
How are you creating these 40 edit fields ?

Personally I would create them all at run time, as members of a
dynamic Array

Alternatively you could set up an array of TEdit and assign each
member of the array to a 'designed' TEdit using FindComponent
Nicholas Sherlock
2005-01-15 08:48:27 UTC
Permalink
Post by Harald Knautzberg
for i := 1 to 40 do
editarray[i] := myarray[i];
I also have to write back the values (on change) in the other array.
Untested, and you should really use this to populate an array so you
don't have to search each time:

for i:=1 to 40 do
tedit(form1.findcomponent('edit'+inttostr(i))).text:='This is edit
number '+inttostr(i);

Cheers,
Nicholas Sherlock
Harald Knautzberg
2005-01-15 09:45:56 UTC
Permalink
Post by Nicholas Sherlock
Untested, and you should really use this to populate an array so you
for i:=1 to 40 do
tedit(form1.findcomponent('edit'+inttostr(i))).text:='This is edit
number '+inttostr(i);
thx, this works fine. I've also declared an const array of strings with
the names of the edit fields, because the names of my edits are not
continuous enumerated they are like edit01, edit02, edit11, edit12,...

but it works very good!

thx!!!

Harry
AlanGLLoyd
2005-02-07 07:54:45 UTC
Permalink
Post by Harald Knautzberg
Post by Nicholas Sherlock
for i:=1 to 40 do
tedit(form1.findcomponent('edit'+inttostr(i))).text:='This is edit
number '+inttostr(i);
thx, this works fine. I've also declared an const array of strings with
the names of the edit fields, because the names of my edits are not
continuous enumerated they are like edit01, edit02, edit11, edit12,...
Let me introduce you to the Format() function which makes strings from
variables in a defined form. For your usage you would use ...

For i := 1 to 40 do
TEdit(Form1.FindComponent(Format('Edit%2d', [i])) := Format('This is Edit
Number %d', [i]);

The "%" is the initial character of a definition of a format and of the type of
a variable from which the string will be created.

The characters following the "%" define the precision and width of the
resultant string, the "d" means that the source type is an integer. The "2d"
means that the string will be left-padded with spaces if the number has less
than 2 digits. The "2.2d" means that the string will be left-padded with zeroes
if the number has less than two digits. If the number has more than two digits
you still get the full number.

All other characters in the format string are repeated in the result. So in
your case you would get "Edit07" for example.

You can use many format definitions in the string, and usually as many
variables. But ...

Format('$%4.4x(%0:%dd)', [MyInteger])

... gives you '$01C8(456d)' - a useful display format of a number in hex and
decimal.

Look up "format strings" in Delphi help.

Alan Lloyd
***@aol.com

Jamie
2005-01-15 22:20:53 UTC
Permalink
Post by Harald Knautzberg
Hello Newsgroup,
I need an array of TEdit declared as a constant but delphi said: error -
undefined designator ed_key1, ed_key2, ed_key3 - constant expression
expected (hope that the translation fits :-)
[code]
unit u_frm_rotoren;
interface
...
Forms,
Dialogs, StdCtrls, Buttons, u_typen;
type
Tfrm_settings = class(TForm)
ed_key1: TEdit;
ed_key2: TEdit;
ed_key3: TEdit;
...
implementation
const ArrEdits : array [0..2] of TEdit
= (ed_key1, ed_key2, ed_key3);
[/code]
Thanks for any ideas,
Harry
classes are dynamic and must be constructed at run time.
a const kind of defeats this.
just simply declare an array of Tedit's and create them
at run time.
like in the form1.create for example.
Loading...