BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

determining TComponent property at runtime

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (VCL Components Usage)
View previous topic :: View next topic  
Author Message
Subrat B.
Guest





PostPosted: Thu Apr 22, 2004 2:57 am    Post subject: determining TComponent property at runtime Reply with quote



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





PostPosted: Thu Apr 22, 2004 3:38 am    Post subject: Re: determining TComponent property at runtime Reply with quote




"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





PostPosted: Thu Apr 22, 2004 8:04 pm    Post subject: Re: determining TComponent property at runtime Reply with quote




"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
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (VCL Components Usage) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.