delphi

核心提示ControlCount可以获得当前控件(可能是Form、Panel, GroupBox)下直接包含的可视化控件总数,通过Controls[i]可以依次访问第i个控件。通过Controls[i] is TButton可以判断控件是不是按钮。

ControlCount可以获得当前控件(可能是Form、Panel, GroupBox)下直接包含的可视化控件总数,通过Controls[i]可以依次访问第i个控件。通过Controls[i] is TButton可以判断控件是不是按钮。

与Controls对应的还有一个ComponentCount和Components。这个是当前窗口下所有控件,不管可见还是不可见,也不分直接包含还是通过Panel,GroupBox等间接包含的控件。

深入解析。

button := TButton.Create(Self); //这里Self.Components中就包含了button,

button.Parent := Self; //这里Self.Controls就包含了button

对你这个问题,想要知道有多少个Button的代码如下:

var

I: Integer;

Count: Integer;

begin

Count := 0;

for I := 0 to ControlCount - 1 do

begin

if Controls[I] is TCustomButton then

Inc(Count);

end;

ShowMessage('所有从TCustomButton继承的控件且直接放在Form上的控件总数为:' + IntToStr(Count));

Count := 0;

for I := 0 to ComponentCount - 1 do

begin

if Components[I] is TCustomButton then

Inc(Count);

end;

ShowMessage('所有从TCustomButton继承的控件总数为:' + IntToStr(Count));

end;

 
友情链接
鄂ICP备19019357号-22