 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Subrat B. Guest
|
Posted: Thu Apr 22, 2004 2:57 am Post subject: determining TComponent property at runtime |
|
|
hi.
How do i determine if TComponent object such as (Textbox,
Tform,Richedit,Button etc etc) can be applied with .caption or .text at
RUNTIME.
i want to make a common function called setText(Tcomponent *x ,AnsiString
a)
which will determine the type of TComponent x and either do
x->Caption = a;
or
x->Text=a;
Thanks
Subrat
|
|
| Back to top |
|
 |
JD Guest
|
Posted: Thu Apr 22, 2004 3:38 am Post subject: Re: determining TComponent property at runtime |
|
|
"Subrat B." <subrat (AT) myktm (DOT) com> wrote:
| Quote: | How do i determine if TComponent object such as (Textbox,
Tform,Richedit,Button etc etc) can be applied with .caption
or .text at RUNTIME.
|
You will have to use dynamic_cast to cast the TComponent to
the appropriate class and then set the corresponding property.
However, I would pass a TControl instead (String - upper case
'S' is shorthand for AnsiString):
void __fastcall TForm1::SetCaption( TControl* pControl, String Text )
{
TForm* pForm = dynamic_cast<TForm*>( pControl );
if( pForm ) pForm->Caption = Text;
else
{
TEdit* pEdit = dynamic_cast<TEdit*>( pControl );
if( pEdit ) pEdit->Text = Text;
else
{
// continue testing
}
}
}
~ JD
|
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Thu Apr 22, 2004 8:04 pm Post subject: Re: determining TComponent property at runtime |
|
|
"Subrat B." <subrat (AT) myktm (DOT) com> wrote
| Quote: | How do i determine if TComponent object such as (Textbox,
Tform,Richedit,Button etc etc) can be applied with .caption
or .text at RUNTIME.
|
You would have to either:
1) use dynamic_cast
void __fastcall SetText(TComponent *Comp, const AnsiString &Str)
{
if( (TEdit *edit = dynamic_cast<TEdit*>(Comp)) != NULL )
edit->Text = Str;
else if( (TButton *btn = dynamic_cast<TButton*>(Comp)) != NULL )
btn->Caption = Str;
// etc...
}
2) use the VCL's RTTI (runtime type information) system (the only problem
with this approach is that it only works with published properties):
#include <TypInfo.hpp>
void __fastcall SetText(TComponent *Comp, const AnsiString &Str)
{
PPropInfo pPropInfo = GetPropInfo(Comp, "Caption");
if( !pPropInfo )
pPropInfo = GetPropInfo(Comp, "Text");
// etc...
if( pPropInfo )
SetStrProp(Comp, pPropInfo, Str);
}
Gambit
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|