Is there any way to get all the controls on a container control?












13















I've got a form with a bunch of controls on it, and I wanted to iterate through all the controls on a certain panel and enable/disable them.



I tried this:



var component: TComponent;
begin
for component in myPanel do
(component as TControl).Enabled := Value;
end;


But that did nothing. Turns out all components are in the form's component collection, not their parent object's. So does anyone know if there's any way to get all the controls inside a control? (Besides an ugly workaround like this, which is what I ended up having to do):



var component: TComponent;
begin
for component in myPanel do
if (component is TControl) and (TControl(component).parent = myPanel) then
TControl(component).Enabled := Value;
end;


Someone please tell me there's a better way...










share|improve this question



























    13















    I've got a form with a bunch of controls on it, and I wanted to iterate through all the controls on a certain panel and enable/disable them.



    I tried this:



    var component: TComponent;
    begin
    for component in myPanel do
    (component as TControl).Enabled := Value;
    end;


    But that did nothing. Turns out all components are in the form's component collection, not their parent object's. So does anyone know if there's any way to get all the controls inside a control? (Besides an ugly workaround like this, which is what I ended up having to do):



    var component: TComponent;
    begin
    for component in myPanel do
    if (component is TControl) and (TControl(component).parent = myPanel) then
    TControl(component).Enabled := Value;
    end;


    Someone please tell me there's a better way...










    share|improve this question

























      13












      13








      13


      3






      I've got a form with a bunch of controls on it, and I wanted to iterate through all the controls on a certain panel and enable/disable them.



      I tried this:



      var component: TComponent;
      begin
      for component in myPanel do
      (component as TControl).Enabled := Value;
      end;


      But that did nothing. Turns out all components are in the form's component collection, not their parent object's. So does anyone know if there's any way to get all the controls inside a control? (Besides an ugly workaround like this, which is what I ended up having to do):



      var component: TComponent;
      begin
      for component in myPanel do
      if (component is TControl) and (TControl(component).parent = myPanel) then
      TControl(component).Enabled := Value;
      end;


      Someone please tell me there's a better way...










      share|improve this question














      I've got a form with a bunch of controls on it, and I wanted to iterate through all the controls on a certain panel and enable/disable them.



      I tried this:



      var component: TComponent;
      begin
      for component in myPanel do
      (component as TControl).Enabled := Value;
      end;


      But that did nothing. Turns out all components are in the form's component collection, not their parent object's. So does anyone know if there's any way to get all the controls inside a control? (Besides an ugly workaround like this, which is what I ended up having to do):



      var component: TComponent;
      begin
      for component in myPanel do
      if (component is TControl) and (TControl(component).parent = myPanel) then
      TControl(component).Enabled := Value;
      end;


      Someone please tell me there's a better way...







      delphi forms controls iterator vcl






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 5 '09 at 23:37









      Mason WheelerMason Wheeler

      62.7k34216405




      62.7k34216405
























          5 Answers
          5






          active

          oldest

          votes


















          25














          You're looking for the TWinControl.Controls array and the accompanying ControlCount property. Those are for a control's immediate children. To get grandchildren etc., use standard recursive techniques.



          You don't really want the Components array (which is what the for-in loop iterates over) since it has nothing to do, in general, with the parent-child relationship. Components can own things that have no child relationship, and controls can have children that they don't own.



          Also note that disabling a control implicitly disables all its children, too. You cannot interact with the children of a disabled control; the OS doesn't send input messages to them. To make them look disabled, though, you'll need to disable them separately. That is, to make a button have grayed text, it's not enough to disable its parent, even though the button won't respond to mouse clicks. You need to disable the button itself to make it paint itself "disabledly."






          share|improve this answer


























          • Thank you. That's exactly what I was looking for.

            – Mason Wheeler
            Jan 5 '09 at 23:56






          • 2





            Once I wrote Controls enumerator. You can find it at 17slon.com/blogs/gabr/2008/02/….

            – gabr
            Jan 6 '09 at 7:05



















          13














          If you disable a panel, al controls on it are disabled too.



          Recursive solution with anonymous methods:



          type
          TControlProc = reference to procedure (const AControl: TControl);

          procedure TForm6.ModifyControl(const AControl: TControl;
          const ARef: TControlProc);
          var
          i : Integer;
          begin
          if AControl=nil then
          Exit;
          if AControl is TWinControl then begin
          for i := 0 to TWinControl(AControl).ControlCount-1 do
          ModifyControl(TWinControl(AControl).Controls[i], ARef);
          end;
          ARef(AControl);
          end;

          procedure TForm6.Button1Click(Sender: TObject);
          begin
          ModifyControl(Panel1,
          procedure (const AControl: TControl)
          begin
          AControl.Enabled := not Panel1.Enabled;
          end
          );
          end;





          share|improve this answer


























          • Very nice. Unfortunately, we don't have D2009 yet here at work. :(

            – Mason Wheeler
            Jan 6 '09 at 19:56











          • And with just minor tweaking (getting rid of TWinControl), can work in Firemonkey too.

            – Jerry Dodge
            Sep 29 '17 at 20:20





















          2














          Here is a Delphi 2007 way:



          procedure TForm6.ModifyControl(const AControl: TControl; const value: Boolean);
          var
          i: Integer;
          begin
          if AControl=nil then Exit;
          if AControl is TWinControl then begin
          for i := 0 to TWinControl(AControl).ControlCount-1 do
          ModifyControl(TWinControl(AControl).Controls[i], value);
          end;
          Acontrol.Enabled := value;
          end;

          procedure TForm6.Button1Click(Sender: TObject);
          begin
          ModifyControl(Panel1, true); // true or false
          end;





          share|improve this answer

































            1














            Simply



            Panel.Enabled := Value;





            share|improve this answer

































              0














              I know this post is a little old but I came here based on a search for the same information. Here is some C++ code that I worked out for anyone interested.



              // DEV-NOTE:  GUIForm flattens the VCL controls
              // VCL controls are nested. I.E. Controls on a
              // Panel would have the Panel as a parent and if
              // that Panel is on a TForm, TForm's control count
              // does not account for the nested controls on the
              // Panel.
              //
              // GUIControl is passed a Form pointer and an index
              // value, the index value will walk the controls on the
              // form and any child controls counting up to the idx
              // value passed in. In this way, every control has a
              // unique index value
              //
              // You can use this to iterate over every single control
              // on a form. Here is example code:
              //
              // int count = 0;
              // TForm *pTForm = some_form
              // TControl *pCtrl = 0;
              // do
              // {
              // pCtrl = GUIControl(pTForm, count++);
              //
              // }while(pCtrl);

              TControl *GUIControl(TForm *F, int idx)
              {
              TControl *rval = 0;
              int RunCount = 0;

              for(int i=0; i<F->ControlCount && !rval; i++)
              {
              TControl *pCtl = F->Controls[i];

              if(RunCount == idx )
              rval = pCtl;
              else
              rval = GUIChildControl( pCtl, RunCount, idx);

              RunCount++;
              }

              return(rval);
              }

              TControl *GUIChildControl(TControl *C, int &runcount, int idx)
              {
              TControl *rval = 0;
              TWinControl *pC = dynamic_cast<TWinControl *>(C);
              if(pC)
              {
              for(int i=0; i<pC->ControlCount && !rval; i++)
              {
              TControl *pCtrl = pC->Controls[i];
              runcount++;

              if( runcount == idx)
              rval = pCtrl;
              else
              {
              TWinControl *pCC = dynamic_cast<TWinControl *>(pCtrl);

              if(pCC)
              {
              if( pCC->ControlCount )
              rval = GUIChildControl(pCtrl, runcount, idx);
              }
              }
              }
              }

              return(rval);
              }





              share|improve this answer
























              • here is a more compact version: stackoverflow.com/questions/2391325/…

                – Rigel
                Oct 10 '18 at 8:09











              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f414928%2fis-there-any-way-to-get-all-the-controls-on-a-container-control%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              5 Answers
              5






              active

              oldest

              votes








              5 Answers
              5






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              25














              You're looking for the TWinControl.Controls array and the accompanying ControlCount property. Those are for a control's immediate children. To get grandchildren etc., use standard recursive techniques.



              You don't really want the Components array (which is what the for-in loop iterates over) since it has nothing to do, in general, with the parent-child relationship. Components can own things that have no child relationship, and controls can have children that they don't own.



              Also note that disabling a control implicitly disables all its children, too. You cannot interact with the children of a disabled control; the OS doesn't send input messages to them. To make them look disabled, though, you'll need to disable them separately. That is, to make a button have grayed text, it's not enough to disable its parent, even though the button won't respond to mouse clicks. You need to disable the button itself to make it paint itself "disabledly."






              share|improve this answer


























              • Thank you. That's exactly what I was looking for.

                – Mason Wheeler
                Jan 5 '09 at 23:56






              • 2





                Once I wrote Controls enumerator. You can find it at 17slon.com/blogs/gabr/2008/02/….

                – gabr
                Jan 6 '09 at 7:05
















              25














              You're looking for the TWinControl.Controls array and the accompanying ControlCount property. Those are for a control's immediate children. To get grandchildren etc., use standard recursive techniques.



              You don't really want the Components array (which is what the for-in loop iterates over) since it has nothing to do, in general, with the parent-child relationship. Components can own things that have no child relationship, and controls can have children that they don't own.



              Also note that disabling a control implicitly disables all its children, too. You cannot interact with the children of a disabled control; the OS doesn't send input messages to them. To make them look disabled, though, you'll need to disable them separately. That is, to make a button have grayed text, it's not enough to disable its parent, even though the button won't respond to mouse clicks. You need to disable the button itself to make it paint itself "disabledly."






              share|improve this answer


























              • Thank you. That's exactly what I was looking for.

                – Mason Wheeler
                Jan 5 '09 at 23:56






              • 2





                Once I wrote Controls enumerator. You can find it at 17slon.com/blogs/gabr/2008/02/….

                – gabr
                Jan 6 '09 at 7:05














              25












              25








              25







              You're looking for the TWinControl.Controls array and the accompanying ControlCount property. Those are for a control's immediate children. To get grandchildren etc., use standard recursive techniques.



              You don't really want the Components array (which is what the for-in loop iterates over) since it has nothing to do, in general, with the parent-child relationship. Components can own things that have no child relationship, and controls can have children that they don't own.



              Also note that disabling a control implicitly disables all its children, too. You cannot interact with the children of a disabled control; the OS doesn't send input messages to them. To make them look disabled, though, you'll need to disable them separately. That is, to make a button have grayed text, it's not enough to disable its parent, even though the button won't respond to mouse clicks. You need to disable the button itself to make it paint itself "disabledly."






              share|improve this answer















              You're looking for the TWinControl.Controls array and the accompanying ControlCount property. Those are for a control's immediate children. To get grandchildren etc., use standard recursive techniques.



              You don't really want the Components array (which is what the for-in loop iterates over) since it has nothing to do, in general, with the parent-child relationship. Components can own things that have no child relationship, and controls can have children that they don't own.



              Also note that disabling a control implicitly disables all its children, too. You cannot interact with the children of a disabled control; the OS doesn't send input messages to them. To make them look disabled, though, you'll need to disable them separately. That is, to make a button have grayed text, it's not enough to disable its parent, even though the button won't respond to mouse clicks. You need to disable the button itself to make it paint itself "disabledly."







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Jan 5 '09 at 23:47

























              answered Jan 5 '09 at 23:41









              Rob KennedyRob Kennedy

              145k16228406




              145k16228406













              • Thank you. That's exactly what I was looking for.

                – Mason Wheeler
                Jan 5 '09 at 23:56






              • 2





                Once I wrote Controls enumerator. You can find it at 17slon.com/blogs/gabr/2008/02/….

                – gabr
                Jan 6 '09 at 7:05



















              • Thank you. That's exactly what I was looking for.

                – Mason Wheeler
                Jan 5 '09 at 23:56






              • 2





                Once I wrote Controls enumerator. You can find it at 17slon.com/blogs/gabr/2008/02/….

                – gabr
                Jan 6 '09 at 7:05

















              Thank you. That's exactly what I was looking for.

              – Mason Wheeler
              Jan 5 '09 at 23:56





              Thank you. That's exactly what I was looking for.

              – Mason Wheeler
              Jan 5 '09 at 23:56




              2




              2





              Once I wrote Controls enumerator. You can find it at 17slon.com/blogs/gabr/2008/02/….

              – gabr
              Jan 6 '09 at 7:05





              Once I wrote Controls enumerator. You can find it at 17slon.com/blogs/gabr/2008/02/….

              – gabr
              Jan 6 '09 at 7:05













              13














              If you disable a panel, al controls on it are disabled too.



              Recursive solution with anonymous methods:



              type
              TControlProc = reference to procedure (const AControl: TControl);

              procedure TForm6.ModifyControl(const AControl: TControl;
              const ARef: TControlProc);
              var
              i : Integer;
              begin
              if AControl=nil then
              Exit;
              if AControl is TWinControl then begin
              for i := 0 to TWinControl(AControl).ControlCount-1 do
              ModifyControl(TWinControl(AControl).Controls[i], ARef);
              end;
              ARef(AControl);
              end;

              procedure TForm6.Button1Click(Sender: TObject);
              begin
              ModifyControl(Panel1,
              procedure (const AControl: TControl)
              begin
              AControl.Enabled := not Panel1.Enabled;
              end
              );
              end;





              share|improve this answer


























              • Very nice. Unfortunately, we don't have D2009 yet here at work. :(

                – Mason Wheeler
                Jan 6 '09 at 19:56











              • And with just minor tweaking (getting rid of TWinControl), can work in Firemonkey too.

                – Jerry Dodge
                Sep 29 '17 at 20:20


















              13














              If you disable a panel, al controls on it are disabled too.



              Recursive solution with anonymous methods:



              type
              TControlProc = reference to procedure (const AControl: TControl);

              procedure TForm6.ModifyControl(const AControl: TControl;
              const ARef: TControlProc);
              var
              i : Integer;
              begin
              if AControl=nil then
              Exit;
              if AControl is TWinControl then begin
              for i := 0 to TWinControl(AControl).ControlCount-1 do
              ModifyControl(TWinControl(AControl).Controls[i], ARef);
              end;
              ARef(AControl);
              end;

              procedure TForm6.Button1Click(Sender: TObject);
              begin
              ModifyControl(Panel1,
              procedure (const AControl: TControl)
              begin
              AControl.Enabled := not Panel1.Enabled;
              end
              );
              end;





              share|improve this answer


























              • Very nice. Unfortunately, we don't have D2009 yet here at work. :(

                – Mason Wheeler
                Jan 6 '09 at 19:56











              • And with just minor tweaking (getting rid of TWinControl), can work in Firemonkey too.

                – Jerry Dodge
                Sep 29 '17 at 20:20
















              13












              13








              13







              If you disable a panel, al controls on it are disabled too.



              Recursive solution with anonymous methods:



              type
              TControlProc = reference to procedure (const AControl: TControl);

              procedure TForm6.ModifyControl(const AControl: TControl;
              const ARef: TControlProc);
              var
              i : Integer;
              begin
              if AControl=nil then
              Exit;
              if AControl is TWinControl then begin
              for i := 0 to TWinControl(AControl).ControlCount-1 do
              ModifyControl(TWinControl(AControl).Controls[i], ARef);
              end;
              ARef(AControl);
              end;

              procedure TForm6.Button1Click(Sender: TObject);
              begin
              ModifyControl(Panel1,
              procedure (const AControl: TControl)
              begin
              AControl.Enabled := not Panel1.Enabled;
              end
              );
              end;





              share|improve this answer















              If you disable a panel, al controls on it are disabled too.



              Recursive solution with anonymous methods:



              type
              TControlProc = reference to procedure (const AControl: TControl);

              procedure TForm6.ModifyControl(const AControl: TControl;
              const ARef: TControlProc);
              var
              i : Integer;
              begin
              if AControl=nil then
              Exit;
              if AControl is TWinControl then begin
              for i := 0 to TWinControl(AControl).ControlCount-1 do
              ModifyControl(TWinControl(AControl).Controls[i], ARef);
              end;
              ARef(AControl);
              end;

              procedure TForm6.Button1Click(Sender: TObject);
              begin
              ModifyControl(Panel1,
              procedure (const AControl: TControl)
              begin
              AControl.Enabled := not Panel1.Enabled;
              end
              );
              end;






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Jan 6 '09 at 0:01

























              answered Jan 5 '09 at 23:44









              Toon KrijtheToon Krijthe

              47k22127191




              47k22127191













              • Very nice. Unfortunately, we don't have D2009 yet here at work. :(

                – Mason Wheeler
                Jan 6 '09 at 19:56











              • And with just minor tweaking (getting rid of TWinControl), can work in Firemonkey too.

                – Jerry Dodge
                Sep 29 '17 at 20:20





















              • Very nice. Unfortunately, we don't have D2009 yet here at work. :(

                – Mason Wheeler
                Jan 6 '09 at 19:56











              • And with just minor tweaking (getting rid of TWinControl), can work in Firemonkey too.

                – Jerry Dodge
                Sep 29 '17 at 20:20



















              Very nice. Unfortunately, we don't have D2009 yet here at work. :(

              – Mason Wheeler
              Jan 6 '09 at 19:56





              Very nice. Unfortunately, we don't have D2009 yet here at work. :(

              – Mason Wheeler
              Jan 6 '09 at 19:56













              And with just minor tweaking (getting rid of TWinControl), can work in Firemonkey too.

              – Jerry Dodge
              Sep 29 '17 at 20:20







              And with just minor tweaking (getting rid of TWinControl), can work in Firemonkey too.

              – Jerry Dodge
              Sep 29 '17 at 20:20













              2














              Here is a Delphi 2007 way:



              procedure TForm6.ModifyControl(const AControl: TControl; const value: Boolean);
              var
              i: Integer;
              begin
              if AControl=nil then Exit;
              if AControl is TWinControl then begin
              for i := 0 to TWinControl(AControl).ControlCount-1 do
              ModifyControl(TWinControl(AControl).Controls[i], value);
              end;
              Acontrol.Enabled := value;
              end;

              procedure TForm6.Button1Click(Sender: TObject);
              begin
              ModifyControl(Panel1, true); // true or false
              end;





              share|improve this answer






























                2














                Here is a Delphi 2007 way:



                procedure TForm6.ModifyControl(const AControl: TControl; const value: Boolean);
                var
                i: Integer;
                begin
                if AControl=nil then Exit;
                if AControl is TWinControl then begin
                for i := 0 to TWinControl(AControl).ControlCount-1 do
                ModifyControl(TWinControl(AControl).Controls[i], value);
                end;
                Acontrol.Enabled := value;
                end;

                procedure TForm6.Button1Click(Sender: TObject);
                begin
                ModifyControl(Panel1, true); // true or false
                end;





                share|improve this answer




























                  2












                  2








                  2







                  Here is a Delphi 2007 way:



                  procedure TForm6.ModifyControl(const AControl: TControl; const value: Boolean);
                  var
                  i: Integer;
                  begin
                  if AControl=nil then Exit;
                  if AControl is TWinControl then begin
                  for i := 0 to TWinControl(AControl).ControlCount-1 do
                  ModifyControl(TWinControl(AControl).Controls[i], value);
                  end;
                  Acontrol.Enabled := value;
                  end;

                  procedure TForm6.Button1Click(Sender: TObject);
                  begin
                  ModifyControl(Panel1, true); // true or false
                  end;





                  share|improve this answer















                  Here is a Delphi 2007 way:



                  procedure TForm6.ModifyControl(const AControl: TControl; const value: Boolean);
                  var
                  i: Integer;
                  begin
                  if AControl=nil then Exit;
                  if AControl is TWinControl then begin
                  for i := 0 to TWinControl(AControl).ControlCount-1 do
                  ModifyControl(TWinControl(AControl).Controls[i], value);
                  end;
                  Acontrol.Enabled := value;
                  end;

                  procedure TForm6.Button1Click(Sender: TObject);
                  begin
                  ModifyControl(Panel1, true); // true or false
                  end;






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Mar 25 '12 at 11:25









                  NGLN

                  37k787153




                  37k787153










                  answered Mar 24 '12 at 18:46









                  Selim Serhat ÇelikSelim Serhat Çelik

                  211




                  211























                      1














                      Simply



                      Panel.Enabled := Value;





                      share|improve this answer






























                        1














                        Simply



                        Panel.Enabled := Value;





                        share|improve this answer




























                          1












                          1








                          1







                          Simply



                          Panel.Enabled := Value;





                          share|improve this answer















                          Simply



                          Panel.Enabled := Value;






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Aug 27 '13 at 7:55









                          bluish

                          13.8k1693147




                          13.8k1693147










                          answered Jan 6 '09 at 1:36









                          dmajkicdmajkic

                          3,04211324




                          3,04211324























                              0














                              I know this post is a little old but I came here based on a search for the same information. Here is some C++ code that I worked out for anyone interested.



                              // DEV-NOTE:  GUIForm flattens the VCL controls
                              // VCL controls are nested. I.E. Controls on a
                              // Panel would have the Panel as a parent and if
                              // that Panel is on a TForm, TForm's control count
                              // does not account for the nested controls on the
                              // Panel.
                              //
                              // GUIControl is passed a Form pointer and an index
                              // value, the index value will walk the controls on the
                              // form and any child controls counting up to the idx
                              // value passed in. In this way, every control has a
                              // unique index value
                              //
                              // You can use this to iterate over every single control
                              // on a form. Here is example code:
                              //
                              // int count = 0;
                              // TForm *pTForm = some_form
                              // TControl *pCtrl = 0;
                              // do
                              // {
                              // pCtrl = GUIControl(pTForm, count++);
                              //
                              // }while(pCtrl);

                              TControl *GUIControl(TForm *F, int idx)
                              {
                              TControl *rval = 0;
                              int RunCount = 0;

                              for(int i=0; i<F->ControlCount && !rval; i++)
                              {
                              TControl *pCtl = F->Controls[i];

                              if(RunCount == idx )
                              rval = pCtl;
                              else
                              rval = GUIChildControl( pCtl, RunCount, idx);

                              RunCount++;
                              }

                              return(rval);
                              }

                              TControl *GUIChildControl(TControl *C, int &runcount, int idx)
                              {
                              TControl *rval = 0;
                              TWinControl *pC = dynamic_cast<TWinControl *>(C);
                              if(pC)
                              {
                              for(int i=0; i<pC->ControlCount && !rval; i++)
                              {
                              TControl *pCtrl = pC->Controls[i];
                              runcount++;

                              if( runcount == idx)
                              rval = pCtrl;
                              else
                              {
                              TWinControl *pCC = dynamic_cast<TWinControl *>(pCtrl);

                              if(pCC)
                              {
                              if( pCC->ControlCount )
                              rval = GUIChildControl(pCtrl, runcount, idx);
                              }
                              }
                              }
                              }

                              return(rval);
                              }





                              share|improve this answer
























                              • here is a more compact version: stackoverflow.com/questions/2391325/…

                                – Rigel
                                Oct 10 '18 at 8:09
















                              0














                              I know this post is a little old but I came here based on a search for the same information. Here is some C++ code that I worked out for anyone interested.



                              // DEV-NOTE:  GUIForm flattens the VCL controls
                              // VCL controls are nested. I.E. Controls on a
                              // Panel would have the Panel as a parent and if
                              // that Panel is on a TForm, TForm's control count
                              // does not account for the nested controls on the
                              // Panel.
                              //
                              // GUIControl is passed a Form pointer and an index
                              // value, the index value will walk the controls on the
                              // form and any child controls counting up to the idx
                              // value passed in. In this way, every control has a
                              // unique index value
                              //
                              // You can use this to iterate over every single control
                              // on a form. Here is example code:
                              //
                              // int count = 0;
                              // TForm *pTForm = some_form
                              // TControl *pCtrl = 0;
                              // do
                              // {
                              // pCtrl = GUIControl(pTForm, count++);
                              //
                              // }while(pCtrl);

                              TControl *GUIControl(TForm *F, int idx)
                              {
                              TControl *rval = 0;
                              int RunCount = 0;

                              for(int i=0; i<F->ControlCount && !rval; i++)
                              {
                              TControl *pCtl = F->Controls[i];

                              if(RunCount == idx )
                              rval = pCtl;
                              else
                              rval = GUIChildControl( pCtl, RunCount, idx);

                              RunCount++;
                              }

                              return(rval);
                              }

                              TControl *GUIChildControl(TControl *C, int &runcount, int idx)
                              {
                              TControl *rval = 0;
                              TWinControl *pC = dynamic_cast<TWinControl *>(C);
                              if(pC)
                              {
                              for(int i=0; i<pC->ControlCount && !rval; i++)
                              {
                              TControl *pCtrl = pC->Controls[i];
                              runcount++;

                              if( runcount == idx)
                              rval = pCtrl;
                              else
                              {
                              TWinControl *pCC = dynamic_cast<TWinControl *>(pCtrl);

                              if(pCC)
                              {
                              if( pCC->ControlCount )
                              rval = GUIChildControl(pCtrl, runcount, idx);
                              }
                              }
                              }
                              }

                              return(rval);
                              }





                              share|improve this answer
























                              • here is a more compact version: stackoverflow.com/questions/2391325/…

                                – Rigel
                                Oct 10 '18 at 8:09














                              0












                              0








                              0







                              I know this post is a little old but I came here based on a search for the same information. Here is some C++ code that I worked out for anyone interested.



                              // DEV-NOTE:  GUIForm flattens the VCL controls
                              // VCL controls are nested. I.E. Controls on a
                              // Panel would have the Panel as a parent and if
                              // that Panel is on a TForm, TForm's control count
                              // does not account for the nested controls on the
                              // Panel.
                              //
                              // GUIControl is passed a Form pointer and an index
                              // value, the index value will walk the controls on the
                              // form and any child controls counting up to the idx
                              // value passed in. In this way, every control has a
                              // unique index value
                              //
                              // You can use this to iterate over every single control
                              // on a form. Here is example code:
                              //
                              // int count = 0;
                              // TForm *pTForm = some_form
                              // TControl *pCtrl = 0;
                              // do
                              // {
                              // pCtrl = GUIControl(pTForm, count++);
                              //
                              // }while(pCtrl);

                              TControl *GUIControl(TForm *F, int idx)
                              {
                              TControl *rval = 0;
                              int RunCount = 0;

                              for(int i=0; i<F->ControlCount && !rval; i++)
                              {
                              TControl *pCtl = F->Controls[i];

                              if(RunCount == idx )
                              rval = pCtl;
                              else
                              rval = GUIChildControl( pCtl, RunCount, idx);

                              RunCount++;
                              }

                              return(rval);
                              }

                              TControl *GUIChildControl(TControl *C, int &runcount, int idx)
                              {
                              TControl *rval = 0;
                              TWinControl *pC = dynamic_cast<TWinControl *>(C);
                              if(pC)
                              {
                              for(int i=0; i<pC->ControlCount && !rval; i++)
                              {
                              TControl *pCtrl = pC->Controls[i];
                              runcount++;

                              if( runcount == idx)
                              rval = pCtrl;
                              else
                              {
                              TWinControl *pCC = dynamic_cast<TWinControl *>(pCtrl);

                              if(pCC)
                              {
                              if( pCC->ControlCount )
                              rval = GUIChildControl(pCtrl, runcount, idx);
                              }
                              }
                              }
                              }

                              return(rval);
                              }





                              share|improve this answer













                              I know this post is a little old but I came here based on a search for the same information. Here is some C++ code that I worked out for anyone interested.



                              // DEV-NOTE:  GUIForm flattens the VCL controls
                              // VCL controls are nested. I.E. Controls on a
                              // Panel would have the Panel as a parent and if
                              // that Panel is on a TForm, TForm's control count
                              // does not account for the nested controls on the
                              // Panel.
                              //
                              // GUIControl is passed a Form pointer and an index
                              // value, the index value will walk the controls on the
                              // form and any child controls counting up to the idx
                              // value passed in. In this way, every control has a
                              // unique index value
                              //
                              // You can use this to iterate over every single control
                              // on a form. Here is example code:
                              //
                              // int count = 0;
                              // TForm *pTForm = some_form
                              // TControl *pCtrl = 0;
                              // do
                              // {
                              // pCtrl = GUIControl(pTForm, count++);
                              //
                              // }while(pCtrl);

                              TControl *GUIControl(TForm *F, int idx)
                              {
                              TControl *rval = 0;
                              int RunCount = 0;

                              for(int i=0; i<F->ControlCount && !rval; i++)
                              {
                              TControl *pCtl = F->Controls[i];

                              if(RunCount == idx )
                              rval = pCtl;
                              else
                              rval = GUIChildControl( pCtl, RunCount, idx);

                              RunCount++;
                              }

                              return(rval);
                              }

                              TControl *GUIChildControl(TControl *C, int &runcount, int idx)
                              {
                              TControl *rval = 0;
                              TWinControl *pC = dynamic_cast<TWinControl *>(C);
                              if(pC)
                              {
                              for(int i=0; i<pC->ControlCount && !rval; i++)
                              {
                              TControl *pCtrl = pC->Controls[i];
                              runcount++;

                              if( runcount == idx)
                              rval = pCtrl;
                              else
                              {
                              TWinControl *pCC = dynamic_cast<TWinControl *>(pCtrl);

                              if(pCC)
                              {
                              if( pCC->ControlCount )
                              rval = GUIChildControl(pCtrl, runcount, idx);
                              }
                              }
                              }
                              }

                              return(rval);
                              }






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Dec 4 '13 at 16:43









                              EricEric

                              1,03842538




                              1,03842538













                              • here is a more compact version: stackoverflow.com/questions/2391325/…

                                – Rigel
                                Oct 10 '18 at 8:09



















                              • here is a more compact version: stackoverflow.com/questions/2391325/…

                                – Rigel
                                Oct 10 '18 at 8:09

















                              here is a more compact version: stackoverflow.com/questions/2391325/…

                              – Rigel
                              Oct 10 '18 at 8:09





                              here is a more compact version: stackoverflow.com/questions/2391325/…

                              – Rigel
                              Oct 10 '18 at 8:09


















                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f414928%2fis-there-any-way-to-get-all-the-controls-on-a-container-control%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Bressuire

                              Vorschmack

                              Quarantine