Board index » delphi » How to reference a visual control via a variable

How to reference a visual control via a variable

Hi

I have a string variable containing the name of a control. How do I
use this variable to indirectly access the control itself, eg-

Temp = 'editEDITBOX';
Temp.Text := 'My text here';

Many thanks,

Alex Chambers

 

Re:How to reference a visual control via a variable


"Dr Alex Chambers" <alichamber...@hotmail.com> wrote in message
news:c57f2443.0110010312.de2b84d@posting.google.com...

Quote
> Hi

> I have a string variable containing the name of a control. How do I
> use this variable to indirectly access the control itself, eg-

> Temp = 'editEDITBOX';
> Temp.Text := 'My text here';

(FindComponent ('editEditBox') as tEdit).Text := 'My text here';

or

tEdit (FindComponent ('editEditBox')).Text := 'My text here';

Re:How to reference a visual control via a variable


Hi

That works. Many thanks. However I still have to state the control's
type in eg- AS tEdit. Is there any way of just referencing the control
via the following, without having to put the tEdit bit in?

Temp = 'editEDITBOX';
Temp.Text := 'My text here';

(FindComponent ('editEditBox') as tEdit).Text := 'My text here';
or
tEdit (FindComponent ('editEditBox')).Text := 'My text here';

Alex

Re:How to reference a visual control via a variable


"Dr Alex Chambers" <alichamber...@hotmail.com> wrote in message
news:c57f2443.0110011540.4f2346fc@posting.google.com...

Quote
> Hi

> That works. Many thanks. However I still have to state the control's
> type in eg- AS tEdit. Is there any way of just referencing the control
> via the following, without having to put the tEdit bit in?

No. tEdit (...) is a "cast" since the FindComponent return value is of type
tComponent. There is no run-time overhead involved in the cast. One is
meerly informing the compiler that the return value can be treated as a
tEdit. The form (... as tEdit) however does involve run-time overhead since
it forces a type check at each execution and will raise an exception if the
instance is not a tEdit (or descendant). All of this is well documented in
the help. I'd suggest that you take the time to read the Object Pascal
Reference section - see the help table of contents.

Re:How to reference a visual control via a variable


In article <c57f2443.0110011540.4f234...@posting.google.com>,
alichamber...@hotmail.com says...

Quote
>> Temp = 'editEDITBOX';
>> Temp.Text := 'My text here';

>> (FindComponent ('editEditBox') as tEdit).Text := 'My text here';
>> or
>> tEdit (FindComponent ('editEditBox')).Text := 'My text here';
> That works. Many thanks. However I still have to state the control's
> type in eg- AS tEdit. Is there any way of just referencing the control
> via the following, without having to put the tEdit bit in?

Sure. You will have to work with RTTI. It is not simple.

Pedja

Other Threads