Recent

Author Topic: How to set Caption property using FindComponent  (Read 9014 times)

fafastrungen

  • New Member
  • *
  • Posts: 24
How to set Caption property using FindComponent
« on: January 07, 2013, 12:11:41 pm »
Hi everyone, I'm making a language manager component, something like Delphi's TSilLang and I'm facing a problem.
I use FindComponent to find components, but this functions returns TComponent type. The problem is when I need to assign a property, for instance Caption property, because TComponent type hasn't Caption property.

Let's assume a label called lblTitle. If I search this label using FindComponent function, I can reach to it, but once I've reached it I can't not access its properties because FindComponent giveme a TComponent object.

Of course I can solve this casting in this way: TLabel(Comp).Caption := '?????'; but this is not right because I don't know if the result of FindComponent is a TLabel object.

I can use SetPropValue but not always, because its not working on every property, for example Memo.Lines.Text

I store languages in an ini file, something like this:

[frmMain]
lblTitle.Caption=This is the Title
btnExit.Caption=Exit
btnExit.Hint=Terminate the app
...


I hope I was clear enough.

Thanks in advance.

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: How to set Caption property using FindComponent
« Reply #1 on: January 07, 2013, 12:21:22 pm »
You are propably after this:
Code: [Select]
if Comp is TLabel then
  TLabel(Comp).Caption := '?????';

fafastrungen

  • New Member
  • *
  • Posts: 24
Re: How to set Caption property using FindComponent
« Reply #2 on: January 07, 2013, 12:26:21 pm »
You are propably after this:
Code: [Select]
if Comp is TLabel then
  TLabel(Comp).Caption := '?????';

Yes and no, because in this way I have to make an IF for every component:

if Comp is TLabel then TLabel(Comp).Caption := '?????';
if Comp is TButton then TLabel(Comp).Caption := '?????';
if Comp is TStaticText then TLabel(Comp).Caption := '?????';
...

there must be another easy way.

thanks for the help.




User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: How to set Caption property using FindComponent
« Reply #3 on: January 07, 2013, 12:43:34 pm »
Don't you need to make "if, else if"'s anyway for each unique component type? TLabel(Comp).Caption is TLabel specific property, nothing general. Case structure might not work with object type as variable.

fafastrungen

  • New Member
  • *
  • Posts: 24
Re: How to set Caption property using FindComponent
« Reply #4 on: January 07, 2013, 12:48:42 pm »
Don't you need to make "if, else if"'s anyway for each unique component type? TLabel(Comp).Caption is TLabel specific property, nothing general. Case structure might not work with object type as variable.

Caption is not TLabel property specific, buttons; panels; speedbutton; ttoolbuttons; ttoglebox; tpanels and others has Caption as property.
I'm trying to avoid every "if/else" in every component when a property is common.

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: How to set Caption property using FindComponent
« Reply #5 on: January 07, 2013, 01:01:23 pm »
If those components would be inherited from more general class which has the original Caption, then you can assign Comp to that. Such as TCustomComponent(Comp).Caption:=... , only that TCustomComponent here is fictional. Each component defines its own Caption if i recall, without inheritance. With Lazarus you can dig those component properties much deeper, i am not on my home computer now to check.

Some general classes are TWinControl, TControl, TStrings...

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: How to set Caption property using FindComponent
« Reply #6 on: January 07, 2013, 01:23:41 pm »
There are 2 ways that I am aware of to do that the first has mentioned by user137 with a small twist you first find the common ancestor that supports the proeprty you are after and use that instead of each component eg.

The  property caption it is declared in the TControl class, this means that you can write something along the lines

if Comp is TControl then TControl(Comp).Caption := '?????';

Which will cover almost all the components that support the caption property. But you have the same problem with other properties which you might need. the correct way is to use the setpropvalue as you suggested with a small twist you must be aware of the number of dots and first retrieve the object reference of the property and then use the setpropvalue on that but that also will not work on objects that do not have any rtti information if I remember correctly only TPersistent based objects have RTTI information which rules out all the TString derivatives where you have to do all the work manually.

In sort there is no unified way to accomplish what you are after for everything but SetPropvalue is closer to your goals with a smaller if ... then ... else tree
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

fafastrungen

  • New Member
  • *
  • Posts: 24
Re: How to set Caption property using FindComponent
« Reply #7 on: January 08, 2013, 11:35:46 pm »
Thanks taazz, what you said is what I'm doing. When I finish the component I'll publish for free.

 

TinyPortal © 2005-2018