for i:=0 to Form1.ComponentCount-1 do begin Form1.Components[i]......end;
同意楼上的。 procedure TForm1.Button2Click(Sender: TObject); var i:Integer; begin for i:=1 ComponentCount-1 do begin Memo1.Lines.Add(Components[i].Name); end; end;
for i:=0 to ComponentCount-1 do Showmessage(Components[i].Classname);
for i:=1 to Form1.ControlCount do Begin Form1.Controls[i].xxxxxx End;
i:Integer; begin for i:=1 ComponentCount-1 do begin ...Components[i]....; end; Components是vcl的全局因子,让你可以遍历vcl组件 ComponentCount是当前vcl组件的个数。
呵呵,俺说两句:显然不是所有的VCL组件都以Form1为Owner。要证明这一点,可以右键点击Form1,选择“View as text”,看看组件之间的创建顺序和父子关系就明白了。要列出Form1中所有的孩子组件,就象查找指定文件夹中的所有文件(包括子文件夹)一样,是一个多重查找。楼上已经有关键的查找方法了,俺就不罗嗦了。
打开对象浏览器观察form的结构,会发现是一棵树,你就把它当作一棵树来遍历就可以了。 做一个递归就可以了 unit test interfaceprocedure DigIntoContainer(Container: TComponent);implementationvar Components: TStrings;procedure DigIntoContainer(Container: TComponent) //只有component的子类才可能出现在面板上可视化设计 var I: Integer; begin if not Assigned(Components) then Exit; Components.Add(Container.Name); if Container.ComponentCount = 0 then Exit; for I := 1 Container.ComponentCount - 1 do begin if Container.Components[I].ComponentCount = 0 then Component.Add(Container.Components[i].Name) else begin DigIntoContainer(Container.Components[i]); end; end; end;initialization Components := TStringList.Create;finalization FreeAndNil(Components);
hntjc() 我试了,都能找到,你说的应该是Controls吧 unit Unit5;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls;type TMainForm = class(TForm) Button2: TButton; Panel1: TPanel; Edit1: TEdit; Button1: TButton; ListBox1: TListBox; procedure Button2Click(Sender: TObject); private { Private declarations } public { Public declarations } end;var MainForm: TMainForm;implementation{$R *.dfm}procedure TMainForm.Button2Click(Sender: TObject); var I: Integer; begin for I := 0 to ComponentCount - 1 do ListBox1.Items.Add(Components[I].Name); end;end. dfm.object MainForm: TMainForm Left = 192 Top = 107 Width = 544 Height = 375 Caption = 'Form' Color = clBtnFace Font.Charset = ANSI_CHARSET Font.Color = clWindowText Font.Height = -12 Font.Name = 'Arial' Font.Style = [] OldCreateOrder = False PixelsPerInch = 96 TextHeight = 15 object Button2: TButton Left = 272 Top = 272 Width = 75 Height = 25 Caption = 'Button2' TabOrder = 0 OnClick = Button2Click end object Panel1: TPanel Left = 8 Top = 24 Width = 225 Height = 233 Caption = 'Panel1' TabOrder = 1 object Edit1: TEdit Left = 40 Top = 24 Width = 121 Height = 23 TabOrder = 0 Text = 'Edit1' end object Button1: TButton Left = 48 Top = 184 Width = 75 Height = 25 Caption = 'Button1' TabOrder = 1 end object ListBox1: TListBox Left = 40 Top = 64 Width = 121 Height = 97 ItemHeight = 15 TabOrder = 2 end end end
begin Form1.Components[i]......end;
procedure TForm1.Button2Click(Sender: TObject);
var
i:Integer;
begin
for i:=1 ComponentCount-1 do
begin
Memo1.Lines.Add(Components[i].Name);
end;
end;
Showmessage(Components[i].Classname);
Begin
Form1.Controls[i].xxxxxx
End;
begin
for i:=1 ComponentCount-1 do
begin
...Components[i]....;
end;
Components是vcl的全局因子,让你可以遍历vcl组件
ComponentCount是当前vcl组件的个数。
不会的,设计时放的vcl元件都以Form为Owner
所以同意My_first(海浪)的
这里找不到答案吗?来这里试试看!
这里有问必答
http://systemer.51.net/cgi-bin/leoboard.cgi
希望你能在这里找到你满意的答案
=========================================
做一个递归就可以了
unit test
interfaceprocedure DigIntoContainer(Container: TComponent);implementationvar
Components: TStrings;procedure DigIntoContainer(Container: TComponent)
//只有component的子类才可能出现在面板上可视化设计
var
I: Integer;
begin
if not Assigned(Components) then Exit;
Components.Add(Container.Name);
if Container.ComponentCount = 0 then Exit;
for I := 1 Container.ComponentCount - 1 do
begin
if Container.Components[I].ComponentCount = 0 then
Component.Add(Container.Components[i].Name)
else
begin
DigIntoContainer(Container.Components[i]);
end;
end;
end;initialization
Components := TStringList.Create;finalization
FreeAndNil(Components);
我试了,都能找到,你说的应该是Controls吧
unit Unit5;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;type
TMainForm = class(TForm)
Button2: TButton;
Panel1: TPanel;
Edit1: TEdit;
Button1: TButton;
ListBox1: TListBox;
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
MainForm: TMainForm;implementation{$R *.dfm}procedure TMainForm.Button2Click(Sender: TObject);
var
I: Integer;
begin
for I := 0 to ComponentCount - 1 do
ListBox1.Items.Add(Components[I].Name);
end;end.
dfm.object MainForm: TMainForm
Left = 192
Top = 107
Width = 544
Height = 375
Caption = 'Form'
Color = clBtnFace
Font.Charset = ANSI_CHARSET
Font.Color = clWindowText
Font.Height = -12
Font.Name = 'Arial'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 15
object Button2: TButton
Left = 272
Top = 272
Width = 75
Height = 25
Caption = 'Button2'
TabOrder = 0
OnClick = Button2Click
end
object Panel1: TPanel
Left = 8
Top = 24
Width = 225
Height = 233
Caption = 'Panel1'
TabOrder = 1
object Edit1: TEdit
Left = 40
Top = 24
Width = 121
Height = 23
TabOrder = 0
Text = 'Edit1'
end
object Button1: TButton
Left = 48
Top = 184
Width = 75
Height = 25
Caption = 'Button1'
TabOrder = 1
end
object ListBox1: TListBox
Left = 40
Top = 64
Width = 121
Height = 97
ItemHeight = 15
TabOrder = 2
end
end
end
并不说明owner
如果运行时创建的控件指定Owner为别的东西的话就找不到了
object Panel1: TPanel
Left = 8
Top = 24
Width = 225
Height = 233
Caption = 'Panel1'
TabOrder = 1
object Edit1: TEdit
Left = 40
Top = 24
Width = 121
Height = 23
TabOrder = 0
Text = 'Edit1'
end
很明显EDIT1的OWER是PANEL,那用你的FOR NEXT的方法根本遍历不到EDIT1了,只能遍历到PANEL了
试试就知道了另外可以做个实验unit TestComponent;interfaceuses
Windows, Messages, SysUtils, Controls, Classes;type
TTestControl = class(TControl)
private
{ Private declarations }
protected
{ Protected declarations }
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
published
{ Published declarations }
end;procedure Register;implementation
uses Dialogs;procedure Register;
begin
RegisterComponents('GybCtrl', [TTestControl]);
end;{ TTestComponent }constructor TTestControl.Create(AOwner: TComponent);
begin
inherited;
ShowMessage(AOwner.Name);
end;end.
随便弄个控件放在窗体上,无论放在Form上,或放在Panel上
弹出来的都是窗体名称的对话框,也就是说Owner都是窗体