控件获取

guozhixin 2009-10-13 02:16:35
怎样才能获得一个form上的所有控件,包括控件的子控件呢?我做的是给同一种控件的Onclick事件赋值,各位高手帮帮忙吧
...全文
52 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
mjp1234airen4385 2009-10-13
  • 打赏
  • 举报
回复
1楼的对,就是用个递归就行鰳
wzzwwz 2009-10-13
  • 打赏
  • 举报
回复
unit Unit1;

interface

uses
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.
dahai9825 2009-10-13
  • 打赏
  • 举报
回复
参考如下 至于控件的子控件 写个递归了行了

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;

5,392

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧