Delphi Controls (控件)和Components (组件)的区别
Delphi Controls (控件)和Components (组件)的区别
Components //列出该组件拥有的所有组件。
property Components[Index: Integer]: TComponent;
使用组件访问此组件拥有的任何组件,例如窗体拥有的组件。当按编号而不是名称引用所拥有的组件时,Components属性最有用。它也在内部用于迭代处理所有拥有的组件。
索引范围从0到ComponentIndex减1。
Controls //列出所有子控件。 控件是所有子控件的数组
property Controls[Index: Integer]: TControl;
控件是所有子控件的数组。这些都是将此控件列为其父属性的控件。Controls属性便于通过数字而不是名称引用控件的子级。例如,控件可用于迭代所有子控件。
不要混淆Controls属性和Components属性。Controls属性列出作为控件子窗口的所有控件,而Components属性列出它拥有的所有组件。窗体拥有放置在其上的所有组件,因此,即使它们是窗体上控件的子窗口,它们也会出现在窗体的组件属性列表中。
控件是只读属性。
一个容器控件如果被其他控件指定为属主(Owner) 那么 Components 则+1;
一个容器控件如果被其他控件指定为 Parent, 那么 Controls 则+1;
可以简单理解成:组件(Components)包含控件(Controls)
示例:

//滔Roy 2021.06.03
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Panel1: TPanel;
Memo1: TMemo;
Button2: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
i: Integer;
begin
Memo1.Clear;
Memo1.Lines.Add('Self.Components: '+ IntToStr(Self.ComponentCount));
for i := 0 to Self.ComponentCount - 1 do
Memo1.Lines.Add(Components[i].Name);
Memo1.Lines.Add('');
Memo1.Lines.Add('Self.Controls: '+ IntToStr(Self.ControlCount));
for i := 0 to Self.ControlCount - 1 do
Memo1.Lines.Add(Controls[i].Name);
end;
end.
创建时间:2021.06.03 更新时间:
博客园 滔Roy https://www.cnblogs.com/guorongtao 希望内容对你有所帮助,谢谢!
浙公网安备 33010602011771号