Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Managing components array

Posted on 2006-07-18
6
Medium Priority
?
306 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 500 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

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: …
Ever visit a website where you spotted a really cool looking Font, yet couldn't figure out which font family it belonged to, or how to get a copy of it for your own use? This article explains the process of doing exactly that, as well as showing how…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

618 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