• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 309
  • Last Modified:

Managing components array

I need to know how to make a loop on every component of a form. But on each loop I need to know if that component is a TEdit or Checkbox or any other kind of  component.
I made it, but when i need to know if  the component is a checkbox and if its Ckecked property is true, I get an error message that says "Ckecked is not a member of TComponents".
Could you help me with that?

Thank you in advance
0
jrascond
Asked:
jrascond
  • 2
1 Solution
 
Tomas Helgi JohannssonCommented:
  Hi!

This example loops through the Forms Component list  and searches for a button named Button1

for(int i=0; i<Form1->ComponentCount; i++) {
    TComponent* Comp = Form1->Components[i];
    TButton * CurrButton = dynamic_cast<TButton *>(Comp);
    if(CurrButton !=NULL && CurrButton != Button1) {
      // you can now compare the value stored
      // in CurrButton with the value of Button1
    }
  }

Hope this helps.
Regards,
   Tomas Helgi
0
 
pmdwCommented:
Tomas has what you need
Just fyi  if you are only checking for check boxes and edit components
you might want to manually edit the DFM and move them to the top.
Thats to say make them the first objects created in form object.
That way you can break out of the loop asap and don't have to check all components
0
 
AdrianSRUCommented:
I don't know how you are determining what type of control each component is but that is an extremely important part of this.  You will get all sorts of runtime errors if, for example, you try to cast a component to TCheckBox and access the Checked property if the component is in fact a TButton or TEdit.  A simple way to get the type of component is with the ClassName function:

if( AnsiString( Comp->ClassName( ) ) == AnsiString( "TButton" ) )
{
}
else if( AnsiString( Comp->ClassName( ) ) == AnsiString( "TEdit" ) )
{
}
else if( AnsiString( Comp->ClassName( ) ) == AnsiString( "TCheckBox" ) )
{
}


--Adrian
0
 
pmdwCommented:
IMHO
The way Tomas showed is cleaner as the check is done for you by the cast action as
far as I know dynamic_cast is only successful when we cast a class to one of its base
classes. So it will still work if you have a Derived TCheckBox object.

The only caviat would be that you have to check that your pointer it is not NULL
EG
  TButton * CurrButton = dynamic_cast<TButton *>(Comp);
   if(CurrButton !=NULL ) {
      // if dymanic cast fails CurrButton is null
      // else CurrButton is the current TButton object
    }

  TCheckBox * CurrCheckBox = dynamic_cast<TCheckBox *>(Comp);
   if(CurrCheckBox !=NULL ) {
      // if dymanic cast fails CurrCheckBox is null
      // else CurrCheckBox is the current TCheckBox object
    }


0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now