怎样才能获得一个form上的所有控件,包括控件的子控件呢?我做的是给同一种控件的Onclick事件赋值,各位高手帮帮忙吧

解决方案 »

  1.   

    参考如下 至于控件的子控件 写个递归了行了 procedure TForm1.Button1Click(Sender: TObject);
    var
      i:integer;
    begin
      for i:=0 to self.ControlCount-1 do
        if (self.Components[i] is TButton) then
        TButton(self.Components[i]).OnClick:=YouOnClick;
    end;
      

  2.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, ExtCtrls;type
      TForm1 = class(TForm)
        Memo1: TMemo;
        Panel1: TPanel;
        Panel2: TPanel;
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}
    procedure TForm1.Button1Click(Sender: TObject);
      procedure GetControl(AControl: TWinControl);
      var
        i: Integer;
      begin
        Memo1.Lines.Add(AControl.Name);
        if AControl.ControlCount > 0 then
        begin
          for i := 0 to AControl.ControlCount - 1 do
            if AControl is TWinControl then
              GetControl(TWinControl(AControl.Controls[i]));
        end;
      end;
    begin
      GetControl(Self);
    end;end.