参考如下 至于控件的子控件 写个递归了行了 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;
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.
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;
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.