Board index » delphi » FindComponent + FindClass

FindComponent + FindClass

Hi,

I'd like to load strings from a file and then change the caption property of
some components given by the strings.

In the following example
  ComponentName and
  ClassName are strings loaded from a file:

  (Form1.FindComponent(ComponentName) as Class(ClassName)).Caption := ...

This example doesn't work. Can anybody tell me how to realize such a task?
If you can, please send me an example.
I'd be glad about any answer.

Kristian Virkus

 

Re:FindComponent + FindClass


In article <Xns915C242E8E30kristianvirkuswe...@62.153.159.134>, Kristian Virkus
wrote:

Quote
> I'd like to load strings from a file and then change the caption property of
> some components given by the strings.

> In the following example
>   ComponentName and
>   ClassName are strings loaded from a file:
>   (Form1.FindComponent(ComponentName) as Class(ClassName)).Caption := ...

You can dispense with the classname. Use the component name with FindComponent
and then use routines from the Typinfo unit to set the property by name.
You can modify the routine below as you need.

{-- SetControlText ----------------------------------------------------}
{: Set a control Text propery, if it has one. If it does not, try the
   Caption property.
@Param aControl is the control to work on.
@Param aText is the text to copy.

Quote
}{ Created 13.12.2000 by P. Below

-----------------------------------------------------------------------}
Procedure SetControlText( aControl: TControl; Const aText: String );
  Begin { SetControlText }
    Assert( Assigned( aControl ));
    If IsPublishedProp( aControl, 'Text' ) Then
      SetStrProp( aControl, 'Text', aText )
    Else If IsPublishedProp( aControl, 'Caption' ) Then
      SetStrProp( aControl, 'Caption', aText );
  End; { SetControlText }

Quote

> This example doesn't work. Can anybody tell me how to realize such a task?
> If you can, please send me an example.
> I'd be glad about any answer.

> Kristian Virkus

Peter Below (TeamB)  100113.1...@compuserve.com)
No e-mail responses, please, unless explicitly requested!
Note: I'm unable to visit the newsgroups every day at the moment,
so be patient if you don't get a reply immediately.

Re:FindComponent + FindClass


Quote
> Procedure SetControlText( aControl: TControl; Const aText: String );
>   Begin { SetControlText }
>     Assert( Assigned( aControl ));
>     If IsPublishedProp( aControl, 'Text' ) Then
>       SetStrProp( aControl, 'Text', aText )
>     Else If IsPublishedProp( aControl, 'Caption' ) Then
>       SetStrProp( aControl, 'Caption', aText );
>   End; { SetControlText }

I need the dsgnintf unit, right? But I haven't (D5Std). I've also heard
that you are not allowed to compile the unit into exe-files because
it's only for design-time!? Is there a "work-around"?

Kristian

Re:FindComponent + FindClass


In article <Xns915CB3D209187kristianvirkuswe...@62.153.159.134>,

Quote
Kristian Virkus wrote:
> I need the dsgnintf unit, right? B

No, you only need TypInfo in your Uses clause. This is not a
design-time unit.

Peter Below (TeamB)  100113.1...@compuserve.com)
No e-mail responses, please, unless explicitly requested!
Note: I'm unable to visit the newsgroups every day at the moment,
so be patient if you don't get a reply immediately.

Re:FindComponent + FindClass


Quote
> No, you only need TypInfo in your Uses clause. This is not a
> design-time unit.

Thank you. It works fine. But as I said I would like to make a
program reading a file and then setting the properties. May I
use the following example for it or will there be errors
(like stack overflow or exceptions like those if you create
many for example TStrings but don't free them again)?

var Ctrl: TControl;
...
Ctrl := (Form1.FindComponent('Button1') as TControl);
SetControlText(Ctrl, 'Hallo');

Thank you very much!
Kristian Virkus

Re:FindComponent + FindClass


In article <Xns915DBE89FA8F6kristianvirkuswe...@62.153.159.134>,

Quote
Kristian Virkus wrote:
> Thank you. It works fine. But as I said I would like to make a
> program reading a file and then setting the properties. May I
> use the following example for it or will there be errors
> (like stack overflow or exceptions like those if you create
> many for example TStrings but don't free them again)?

> var Ctrl: TControl;
> ....
> Ctrl := (Form1.FindComponent('Button1') as TControl);
> SetControlText(Ctrl, 'Hallo');

This will work fine. You do not actually create any object here, so
there is nothing to free. You don't need to use a TControl, by the way.
The routine i gave you can be rewritten to take a TComponent or
Tobject, the TypInfo routines all work with TObject as parameter type.
I used TControl just because this is one of a set of routines destined
to only be used on visual components in my program.

Peter Below (TeamB)  100113.1...@compuserve.com)
No e-mail responses, please, unless explicitly requested!
Note: I'm unable to visit the newsgroups every day at the moment,
so be patient if you don't get a reply immediately.

Other Threads