SRL Archive - Delphi/FPC Help and Tutorials

question: how do i select a Tedit dynamicly


nielskool

2009-09-13 01:17:12

Heey,

Is it possible to set a variable equalivant to the name of a tedit so i can acces the tedit by using the variable
Like:

var
Editbox : String;

begin
Form1.[Editbox].caption := 'test';
end;

Greetz,
Niels

caused

2009-09-13 01:21:05

You can set a string var to tedit.

If you want it to be synced with your var use a timer that does the job ;).

~caused

nielskool

2009-09-13 01:23:37

i don't realy get it….:confused:
it may be because my english isn't 100%:P
or i just don't know:P

caused

2009-09-13 02:28:47

Its just one way, but its probably the best way, if your using your var in various functions of your porgramme(please correct me if i'm wrong :)).

1. Create a timer.
2. Create a global var that you want to sync to the tedit.
3. in the timer add sth. like "tedit.caption := YOURVAR;"

Done. When something changes YOURVAR now, changes will affect your TEDIT.

~caused

nielskool

2009-09-13 02:45:03

no, no, no:P
i have 2 edit boxes
in both you put a number
than when you click ok, it wil generate a new form, based on the values you've set, so the size of the form changes.
so it can be 2 edit boxes in the new for but it can also be that there are about 15 edit boxes.
then i want to get the values of all of those editboxes…
the names of these Tedits are like t1t1 t1t2 t1t3, t2t1 t2t2 t2t3
how can i simulate the t1t1 with a var
so:
var
thebox : String;
var: array of array of integer;

begin
for x:=0 to high(..) do
begin
for y:=0 to high(..) do
begin
thebox := t+inttostr(x)+ t+inttostr(y);
var[x][y] := form1.thebox.caption;
end;
end;

//edit
if want it in the reverse direction of what you say…:P

//edit
if found something about findcomponent
so it would be:
var
handle : Tcomponent;
test: integer;
begin
handle := findcomponent(thebox)
test := form3.Tedit(handle).caption;

but now it tells me, [Error] Unit2.pas(139): Undeclared identifier: 'FindComponent'

Daniel

2009-09-13 06:27:04

Use an array of TEdits? :p

Sorry, i'm not quite sure what you mean…

nielskool

2009-09-13 12:41:19

there are a few tedti's generated:
these are the names of the tedit's:
t1t1
t1t2
t1t3

t2t1
t2t2
t2t3

now how can i access the tedit by using a var as name?
like:

var
nam : String;

begin
nam:= t1t1
[nam].caption
end;

i hope you now know what i mean

Dan Cardin

2009-09-13 22:56:23

twould be easiest to just use an array of TEdits so you could do edt[0] and then just keep track of the number. Or you could try adding another Tedit and setting them equal to each other. I assume you're needing to keep track of the info from the Tedit itself and the name isnt important.

nielskool

2009-09-14 00:23:34

:(
ok i would use a array…. an unkown amount of tedits… how could i acces them with a for loop??

Dan Cardin

2009-09-14 14:52:32

still not exactly sure what you're doing but
Procedure InititalizeEdits(sender: Tobject);
begin
with Tedit(sender) do
begin
//initialize the values of stuff for instance...
SetBounds(0,0,0,0)
end;
end;

procedure Change(sender:Tobject);
var i: integer;
begin
for i := 0 to high(meow) do
case sender of
meow[0], meow[1], meow[2]: //do stuff
meow[4]: //more stuff
end;
randomvar := tedit(sender).text;
end;

var meow: array of Tedit;
begin
SetArrayLength(meow, a);
for i := 0 to a do
begin
InitializeEdits(meow[i]); //something like that for using it.
Meow[i].OnChange := @Change;
end;
end;

i just kinda used a bunch of ways in which one might use them.

SKy Scripter

2009-09-16 09:28:33

do you mean like.

TEdit(edit1).Text := 'asdsad';

or like do you mean.

var name: tedit;

begin
name := TEdit(edit1);
name.Text := 'sadasd';
end;

?

or
Edits: array ['a'..'z'] of Tedit;

Edits['a'] := Tedit(edit12);
edits['a'].text := 'sadsa';
edits['h'] := tedit(edit21);
edits['h'].text :='sdsa';

nielskool

2009-09-16 19:53:14

i already got it working… dan cardin's way helped me out, it wasn't what i asked, but his way was the solution.

but now i got another prob:PXD
its al working… but at some point it sometimes does a exponential of 100 or bigger:P(i think)
and then il get the error: floating point overflow
how to solve this?
this is my pow function:

function Power2(Base, Exponent : Double) : Double;
{ raises the base to the exponent }
CONST
cTiny = 1e-15;

VAR
Power : Double; { Value before sign correction }

BEGIN
Power := 0;

{ Deal with the near zero special cases }
IF (Abs(Base) < cTiny) THEN BEGIN
Base := 0.0;
END; { IF }
IF (Abs(Exponent) < cTiny) THEN BEGIN
Exponent := 0.0;
END; { IF }

{ Deal with the exactly zero cases }
IF (Base = 0.0) THEN BEGIN
Power := 0.0;
END; { IF }
IF (Exponent = 0.0) THEN BEGIN
Power := 1.0;
END; { IF }

{ Cover everything else }
IF ((Base < 0) AND (Exponent < 0)) THEN
Power := 1/Exp(-Exponent*Ln(-Base))
ELSE IF ((Base < 0) AND (Exponent >= 0)) THEN
Power := Exp(Exponent*Ln(-Base))
ELSE IF ((Base > 0) AND (Exponent < 0)) THEN
Power := 1/Exp(-Exponent*Ln(Base))
ELSE IF ((Base > 0) AND (Exponent >= 0)) THEN
Power := Exp(Exponent*Ln(Base));

{ Correct the sign }
IF ((Base < 0) AND (Frac(Exponent/2.0) <> 0.0)) THEN
Result := -Power
ELSE
Result := Power;
END; { FUNCTION Pow }

pow function i don't use anymore:

Function Pow(Base, Exponent: double): double;
Begin
//ShowMessage(inttostr(base)+'ss'+inttostr(Exponent));
if Exponent = 0 then
result := 1
else
if(Exponent > 0) then
begin
result := Base * Pow(Base, Exponent - 1);
end;
if(Exponent < 0) then
begin
result := Base * Pow(Base, Exponent + 1);
end;
End;

//edit
nobody knows the answer?
i searched on google, but then i find something about math.pow or system.math.pow.. these don't exsist at my delphi 6..:(
so i had to add a source of pow.. but it just isn't fail safe.
so my question is, does anyone know a fail safe pow source or the function of delphi to do it..