大家知道,可以用下面的方法遍历当前form上的所有控件,Components TCompont* 目前Form上指向所有控件的数组 原来的写法:
c:=components[i];
timage(components[i-1]).visible :=true;我现在想只是遍历当前form上的image控件,请问应该怎样写代码?
c:=components[i];
timage(components[i-1]).visible :=true;我现在想只是遍历当前form上的image控件,请问应该怎样写代码?
begin
if Componets[i] is TImage then
TImage(Componets[i] ).Visible := true;
end;
if Components[ii].ClassName = 'TImage' then
ShowMessage(Components[ii].Name);
(Components[i] as TImage).visible:= true;
if Form1.Controls[iLoop].ClassType = 'TImage' then
with (Form1.Controls[iLoop] as TImage) do
begin
....
end;
每个FORM都有一个COMPONENTCOUNT属性,它代表FORM上的控件数目,在我所举的这个例子里,
假设你在一个FORM上放了二个CHECKBOX控件,一个EDIT控件,所以现在FORM上有三个控件,
COMPONENTCOUNT的值为3。
每个FORM又都有一个COMPONENTS属性,它代表FORM上的控件。
请看下面的一段代码:
for icount:=0 to form1.ComponentCount-1 do
begin
if form1.Components[icount] is tcheckbox then
tcheckbox(form1.Components[icount]).checked:=true;
end;
这段代码的意思是循环扫描FORM上的控件,本例中你在一个
FORM上放了二个CHECKBOX控件,一个EDIT控件,所以COMPONENTS[0],
COMPONENTS[1],COMPONENTS[2]分别代表这三个控件。但到底是COMPONENTS[0]是
CHECKBOX呢还是COMPONENTS[1]是CHECKBOX呢,我们不得而知,所以我们用
if form1.Components[icount] is tcheckbox then来判断。
这段代码的意思是如果“COMPONENTS[ICOUNT]是CHECKBOX的话,那么”,下面再用
tcheckbox(form1.Components[icount]).checked:=true;把每个CHECKBOX的CHECKED属性
设置为TRUE。
你明白了吗?我是明白了,呵呵
ControlCount
Controls TControl有没有其它的能够直接解决我提出的问题而不用循环语句?
谢谢!
if components[i-1] is timage then
timage(components[i-1]).visible :=true;