Solved

Managing components array

Posted on 2006-07-18
6
301 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
6 Comments
 
LVL 25

Accepted Solution

by:
Tomas Helgi Johannsson earned 125 total points
ID: 17136734
  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
ID: 17137834
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
ID: 17138005
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
ID: 17139172
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

734 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