Solved

Managing components array

Posted on 2006-07-18
6
288 Views
Last Modified: 2013-12-03
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
Comment
Question by:jrascond
  • 2
6 Comments
 
LVL 24

Accepted Solution

by:
Tomas Helgi Johannsson earned 125 total points
Comment Utility
  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
 
LVL 2

Expert Comment

by:pmdw
Comment Utility
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
 
LVL 12

Expert Comment

by:AdrianSRU
Comment Utility
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
 
LVL 2

Expert Comment

by:pmdw
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now