Delphi Controls (控件)和Components (组件)的区别
<p><span style="font-size: 16px"><strong>Delphi Controls (控件)和Components (组件)的区别<br></strong></span></p><p><span style="font-size: 16px"><strong>Components </strong> //列出该组件拥有的所有组件。</span></p>
<div class="cnblogs_Highlighter">
<pre class="brush:delphi;gutter:true;"><span style="font-size: 16px">property Components: TComponent;</span></pre>
</div>
<p><span style="font-size: 16px">使用组件访问此组件拥有的任何组件,例如窗体拥有的组件。当按编号而不是名称引用所拥有的组件时,Components属性最有用。它也在内部用于迭代处理所有拥有的组件。</span></p>
<p><span style="font-size: 16px">索引范围从0到ComponentIndex减1。</span></p>
<p><span style="font-size: 16px"><strong>Controls </strong> //列出所有子控件。 控件是所有子控件的数组</span></p>
<div class="cnblogs_Highlighter">
<pre class="brush:delphi;gutter:true;"><span style="font-size: 16px">property Controls: TControl;</span></pre>
</div>
<p><span style="font-size: 16px">控件是所有子控件的数组。这些都是将此控件列为其父属性的控件。Controls属性便于通过数字而不是名称引用控件的子级。例如,控件可用于迭代所有子控件。</span></p>
<p><span style="font-size: 16px">不要混淆Controls属性和Components属性。<strong>Controls属性列出作为控件子窗口的所有控件,而Components属性列出它拥有的所有组件。</strong>窗体拥有放置在其上的所有组件,因此,即使它们是窗体上控件的子窗口,它们也会出现在窗体的组件属性列表中。</span></p>
<p><span style="font-size: 16px">控件是只读属性。</span></p>
<p> </p>
<p><span style="font-size: 16px">一个容器控件如果被其他控件指定为属主(Owner) 那么 Components 则+1; </span><br><span style="font-size: 16px">一个容器控件如果被其他控件指定为 Parent, 那么 Controls 则+1; </span></p>
<p> </p>
<p><span style="font-size: 16px">可以简单理解成:<strong>组件(Components)包含控件(Controls)</strong></span></p>
<p> </p>
<p><span style="font-size: 16px">示例:</span></p>
<p><span style="font-size: 16px"> <img src="https://img2020.cnblogs.com/blog/728850/202106/728850-20210603144604367-1283179866.png" alt="" loading="lazy"> </span></p>
<p><span style="font-size: 16px"><img src="https://img2020.cnblogs.com/blog/728850/202106/728850-20210603144022031-1711494079.png" alt="" loading="lazy"></span></p>
<div class="cnblogs_Highlighter">
<pre class="brush:delphi;gutter:true;">//滔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.Name);
Memo1.Lines.Add('');
Memo1.Lines.Add('Self.Controls: '+ IntToStr(Self.ControlCount));
for i := 0 to Self.ControlCount - 1 do
Memo1.Lines.Add(Controls.Name);
end;
end.
</pre>
</div>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p><span style="font-size: 16px"> </span></p>
<p><span style="color: rgba(136, 136, 136, 1)">创建时间:2021.06.03 更新时间:</span></p>
<p> </p>
</div>
<div id="MySignature" role="contentinfo">
博客园 滔Roy https://www.cnblogs.com/guorongtao 希望内容对你有所帮助,谢谢!<br><br>
来源:https://www.cnblogs.com/guorongtao/p/14844929.html
頁:
[1]