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

后端 未结 6 461
小蘑菇
小蘑菇 2020-12-10 04:11

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:



        
6条回答
  •  离开以前
    2020-12-10 04:33

    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;
    

提交回复
热议问题