Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Looping through all Controls in a Container (TPanel)


This question is answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 6 - Last Post: Apr 6, 2017 9:02 AM Last Post By: Remy Lebeau (Te...
Goran Ekstrom

Posts: 149
Registered: 1/10/04
Looping through all Controls in a Container (TPanel)  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 6, 2017 6:49 AM
I am trying to get the pointers to all controls placed on a Container, in this case a TPanel.

I use this code:

for (int i = 0; i < Container->ControlCount; i++)
{
TRadioButton *TheButton = (TRadioButton *)Container->Controls[i];

.... // Do something
}

The Container->ControlCount property shows the right number of controls but...

TRadioButton *TheButton = (TRadioButton *)Container->Controls[i];

... only works for the first control (Container->Controls[0]), then it either hangs or I get NULL.

How do I iterate through the controls in a container?

Regards
Goran

Edited by: Goran Ekstrom on Apr 6, 2017 4:49 PM
Antonio Estevez

Posts: 665
Registered: 4/12/00
Re: Looping through all Controls in a Container (TPanel) [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 6, 2017 7:43 AM   in response to: Goran Ekstrom in response to: Goran Ekstrom
El 06/04/2017 a las 15:50, Goran Ekstrom escribió:
I am trying to get the pointers to all controls placed on a Container, in this case a TPanel.

I use this code:

for (int i = 0; ii < Container->ControlCount; ii++)

Is this a typo? You are declaring "i" as index but you are using "ii"


{
TRadioButton *TheButton = (TRadioButton *)Container->Controls[ii]);

Is the last ")" another typo?


.... // Do something
}

The Container->ControlCount property shows the right number of controls but...

TRadioButton *TheButton = (TRadioButton *)Container->Controls[ii]);

... only works for the first control (Container->Controls[0]), then it either hangs or I get NULL.

How do I iterate through the controls in a container?

Regards
Goran

This works for me in 10.1 Berlin if the panel contains TRadioButton
controls only:

    for (int ii = 0; ii < Container->ControlCount; ii++)
    {
       TRadioButton *TheButton= (TRadioButton *)Container->Controls[ii];
 
    }

If you have more of one type of control in panel you should do it this way:

    for (int ii = 0; ii < Container->ControlCount; ii++)
    {
       TControl *Ctrl= Container->Controls[ii];
       if (TRadioButton *TheButton= dynamic_cast<TRadioButton*>(Ctrl))
       {
          // Use TheButton as needed
       }
/*
       else if (TLabel *TheLabel= dynamic_cast<TLabel*>(Ctrl))
       {
          // Use TheLabel as needed
       }
       else ...
*/
    }
Goran Ekstrom

Posts: 149
Registered: 1/10/04
Re: Looping through all Controls in a Container (TPanel) [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 6, 2017 7:55 AM   in response to: Antonio Estevez in response to: Antonio Estevez
for (int i = 0; ii < Container->ControlCount; ii++)
Is this a typo? You are declaring "i" as index but you are using "ii"

Sorry for the mess, I've cleaned the original post.

This is the complete copy-paste of the code:

TPanel *MatrixGridPanel;
for (int i = 0; i < MAX_MATRIX_OUTPUTS; i++)
	{
	switch(i)
		{
		case 0: { MatrixGridPanel = Output1Panel; break; }
		case 1: { MatrixGridPanel = Output2Panel; break; }
		case 2: { MatrixGridPanel = Output3Panel; break; }
		case 3: { MatrixGridPanel = Output4Panel; break; }
		case 4: { MatrixGridPanel = Output5Panel; break; }
		case 5: { MatrixGridPanel = Output6Panel; break; }
		case 6: { MatrixGridPanel = Output7Panel; break; }
		case 7: { MatrixGridPanel = Output8Panel; break; }
		}
 
	for (int ii = 0; ii < MatrixGridPanel->ComponentCount; ii++)
		{
		int Tag = ((TRadioButton *)MatrixGridPanel->Components[ii])->Tag / 10;
		TRadioButton *OurRB = (TRadioButton *)MatrixGridPanel->Components[ii];
 
		MatrixButtonGrid[i][Tag] = OurRB;
		}
	}


This works for me in 10.1 Berlin if the panel contains TRadioButton
controls only:
    for (int ii = 0; ii < Container->ControlCount; ii++)
    {
       TRadioButton *TheButton= (TRadioButton *)Container->Controls[ii];
 
    }

One moment...

If you have more of one type of control in panel you should do it this way:

I have not, so no dynamic_cast.
Antonio Estevez

Posts: 665
Registered: 4/12/00
Re: Looping through all Controls in a Container (TPanel) [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 6, 2017 8:13 AM   in response to: Goran Ekstrom in response to: Goran Ekstrom
Goran Ekstrom wrote:
for (int i = 0; ii < Container->ControlCount; ii++)
Is this a typo? You are declaring "i" as index but you are using "ii"

Sorry for the mess, I've cleaned the original post.

This is the complete copy-paste of the code:

TPanel *MatrixGridPanel;
for (int i = 0; i < MAX_MATRIX_OUTPUTS; i++)
	{
	switch(i)
		{
		case 0: { MatrixGridPanel = Output1Panel; break; }
		case 1: { MatrixGridPanel = Output2Panel; break; }
		case 2: { MatrixGridPanel = Output3Panel; break; }
		case 3: { MatrixGridPanel = Output4Panel; break; }
		case 4: { MatrixGridPanel = Output5Panel; break; }
		case 5: { MatrixGridPanel = Output6Panel; break; }
		case 6: { MatrixGridPanel = Output7Panel; break; }
		case 7: { MatrixGridPanel = Output8Panel; break; }
		}
 
	for (int ii = 0; ii < MatrixGridPanel->ComponentCount; ii++)
		{
		int Tag = ((TRadioButton *)MatrixGridPanel->Components[ii])->Tag / 10;
		TRadioButton *OurRB = (TRadioButton *)MatrixGridPanel->Components[ii];
 
		MatrixButtonGrid[i][Tag] = OurRB;
		}
	}


You are using the Components list. Components contains the components that the panel owns only, not the component that it contains. Normaly the parent Form is the owner of all components.

You should use the Controls list. Try this:
   for (int ii = 0; ii < MatrixGridPanel->ControlCount; ii++)
   {
      TRadioButton *OurRB = (TRadioButton *)MatrixGridPanel->Controls[ii];
      int Tag = OurRB->Tag / 10;
 
      MatrixButtonGrid[i][Tag] = OurRB;
    }
Goran Ekstrom

Posts: 149
Registered: 1/10/04
Re: Looping through all Controls in a Container (TPanel) [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 6, 2017 8:20 AM   in response to: Antonio Estevez in response to: Antonio Estevez
You are using the Components list. Components contains the components that the panel owns only, not the component that it contains. Normaly the parent Form is the owner of all components.

Yes, when I redesigned the code I noticed that. Anyway, problem solved:

TPanel *MatrixGridPanel;
for (int i = 0; i < MAX_MATRIX_OUTPUTS; i++)
	{
	switch(i)
		{
		case 0: { MatrixGridPanel = Output1Panel; break; }
		case 1: { MatrixGridPanel = Output2Panel; break; }
		case 2: { MatrixGridPanel = Output3Panel; break; }
		case 3: { MatrixGridPanel = Output4Panel; break; }
		case 4: { MatrixGridPanel = Output5Panel; break; }
		case 5: { MatrixGridPanel = Output6Panel; break; }
		case 6: { MatrixGridPanel = Output7Panel; break; }
		case 7: { MatrixGridPanel = Output8Panel; break; }
		}
 
	for (int ii = 0; ii < MatrixGridPanel->ControlCount; ii++)
		{
		TRadioButton *OurRB = (TRadioButton *)MatrixGridPanel->Controls[ii];
 
 
                                    // THIS WAS ORIGINAL PROBLEM:
 
		int Tag = (OurRB->Tag % 10) - 1;
 
                                    // PREVIOUS DESIGN MOST LIKLEY CAUSED OUT OF ARRAY ACCESS
 
 
		MatrixButtonGrid[i][Tag] = OurRB;
		}
	}
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Looping through all Controls in a Container (TPanel) [Edit] [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 6, 2017 9:02 AM   in response to: Antonio Estevez in response to: Antonio Estevez
Antonio wrote:

Normaly the parent Form is the owner of all components.

That is true for controls created at design-time, but for controls created
manually at runtime, the Owner can be anything the programmer wants to use.

--
Remy Lebeau (TeamB)


---
This email has been checked for viruses by AVG.
http://www.avg.com

Martin van der ...

Posts: 57
Registered: 7/14/02
Re: Looping through all Controls in a Container (TPanel)  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 6, 2017 7:43 AM   in response to: Goran Ekstrom in response to: Goran Ekstrom
Goran Ekstrom wrote:
for (int i = 0; ii < Container->ControlCount; ii++)

If this is copy-pasted from your code, check your variable names.

But if that's just a typo here on the forum, this method should work, assuming that all the controls on the panel are in fact TRadioButtons. There must be something else wrong.
You should check the control's type using the ClassName() function, or use dynamic_cast<> instead (which is probably a good idea either way).
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02