Solved

Managing components array

Posted on 2006-07-18
6
291 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 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How do I customize my Standard Colors in PowerPoint 2010? 6 43
Visual Studio 2015 and Uninstall of Web Essentials 2 1,090
Issues with Word font 4 79
Universal fonts 2 149
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…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand columnThat will then direct you to their download page.From that page s…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
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…

863 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

27 Experts available now in Live!

Get 1:1 Help Now