Board index » delphi » Objects created during runtime

Objects created during runtime

How can I access the properties of an object (i.e. EditBox.Text "Test" or
s.th.]) that was created during runtime? I parented the EditBox with a
GroupBox.
So I try to access GroupBox1.Edit1
                                                ^ can't access : undefined
reference (or s.th. like that)

Then I try: X := GroupBox1.Components[0].Text
                                                                    ^
undefined again

Please Help!

R_F...@addcom.de

 

Re:Objects created during runtime


"Ronny Falk" <r_f...@addcom.de> skrev i melding
news:8sebhk$1ge$3@riker.addcom.de...

Quote
> How can I access the properties of an object (i.e. EditBox.Text "Test"
or
> s.th.]) that was created during runtime? I parented the EditBox with a
> GroupBox.
> So I try to access GroupBox1.Edit1
>                                                 ^ can't access :
undefined
> reference (or s.th. like that)

To do this, you need to use FindComponent('Edit1'), but then you need to
explicitly name your edit box. Only designtime created components are
given names:

  TEdit(GroupBox1.FindComponent('Edit1')).Text:='Some text';

...beware that this crashes if a component named 'Edit1' is not found...

Quote
> Then I try: X := GroupBox1.Components[0].Text
>                                                                     ^
> undefined again

You need to typecast the reference, since the Components[0] returns a
TComponent. The TComponent doesn't have a Text property:

   X := TEdit(GroupBox1.Components[0]).Text;

...if you're creating different kinds of controls, use:

  if GroupBox1.Components[0] is TEdit then
   X := TEdit(GroupBox1.Components[0]).Text
  else if GroupBox1.Components[0] is TLabel then
   X := TLabel(GroupBox1.Components[0]).Caption;

--
Bjoerge Saether
Consultant / Developer
Asker, Norway
bsaether.removet...@online.no (remove the obvious)

Re:Objects created during runtime


The message <8sebhk$1g...@riker.addcom.de>
  from  "Ronny Falk" <r_f...@addcom.de> contains these words:

Quote
> How can I access the properties of an object (i.e. EditBox.Text "Test" or
> s.th.]) that was created during runtime? I parented the EditBox with a
> GroupBox.
> So I try to access GroupBox1.Edit1
>                                                 ^ can't access : undefined
> reference (or s.th. like that)
> Then I try: X := GroupBox1.Components[0].Text
>                                                                     ^
> undefined again

Hi,

If you declared your edit box like this:-

Type
     TForm1 = Class(TForm)
     ......
     private
     .......
     Edit1:Tedit;
     .......
     public
     end;

var Form1:TForm1;

Then in FormCreate you can say

TForm1.FormCreate(sender:TOBject);
begin
Edit1:=TEdit.Create(Form1);
Edit1.Parent:=Groupbox1;
Edit1.Visible:=True;
....
end;

Then in any method or event handler
you can access it using it's name.

TForm1.Button1Click(Sender:TOBject);
begin
Edit1.Text:='Hello There';
end;

It's as simple as that!

Quote
> Then I try: X := GroupBox1.Components[0].Text

In order for the compiler to recognise this as a TEdit you have to
use a typecast.

Assuming X is of type string...

X:=TEdit(GroupBox1.Components[0]).Text;

This is only safe to use if you are certain that component 0 in the
array is a TEdit
otherwise you need to say

if (GroupBox1.Components[0]) is TEdit then
    X:=TEdit(GroupBox1.Components[0]).Text;

You also need to check the component count, because if the count is
zero, trying to
access Components[0] will produce an exception.    

--
Regards,

Andreas Kyriacou
----------------
http://www.andrikkos.co.uk

Re:Objects created during runtime


...
var
  e1:TEdit;
begin
 e1 := TEdit.Create(self);  // "self" can also be "nil" as well
 e1.Text := 'blah';
end;
...

mld.

Quote
Ronny Falk <r_f...@addcom.de> wrote in message

news:8sebhk$1ge$3@riker.addcom.de...
Quote
> How can I access the properties of an object (i.e. EditBox.Text "Test" or
> s.th.]) that was created during runtime? I parented the EditBox with a
> GroupBox.
> So I try to access GroupBox1.Edit1
>                                                 ^ can't access : undefined
> reference (or s.th. like that)

> Then I try: X := GroupBox1.Components[0].Text
>                                                                     ^
> undefined again

> Please Help!

> R_F...@addcom.de

Other Threads