为何TPanel不是Controls? ( 积分: 50 )

  • 主题发起人 主题发起人 houtor
  • 开始时间 开始时间
H

houtor

Unregistered / Unconfirmed
GUEST, unregistred user!
有Form1加上几个TSpeedButton和TPanel控件,代码如下:
for i := 0 to Form1.ControlCount-1 do begin
if Form1.Controls is TSpeedButton then
if TSpeedButton(Form1.Controls).Visible then
TSpeedButton(Form1.Controls).Visible:=False
else TSpeedButton(Form1.Controls).Visible:=True
else if Form1.Controls is TPanel then
if TPanel(Form1.Controls).Visible then
TPanel(Form1.Controls).Visible:=False
else TPanel(Form1.Controls).Visible:=True
end;
TSpeedButton可以,但if Form1.Controls is TPanel then下的根本不运行,难道TPanel控件不是Controls类型的吗(Form1.Controls is TPanel = false呀),如何改呀?
 
大概你没有引用TPanel所在的ExtCtrls单元吧。
 
Tpanel肯定是TControl的子类, 也许那几个Panel的parent不是Form1吧,另外这样好象更简单
for i := 0 to Form1.ControlCount-1 do
if Form1.Controls is TSpeedButton then
TSpeedButton(Form1.Controls).Visible:= not TSpeedButton(Form1.Controls).Visible
else if Form1.Controls is TPanel then
TPanel(Form1.Controls).Visible:= TPanel(Form1.Controls).Visible;
 
Tpanel肯定是TControl的子类, 也许那几个Panel的parent不是Form1吧,另外这样好象更简单
for i := 0 to Form1.ControlCount-1 do
if Form1.Controls is TSpeedButton then
TSpeedButton(Form1.Controls).Visible:= not TSpeedButton(Form1.Controls).Visible
else if Form1.Controls is TPanel then
TPanel(Form1.Controls).Visible:= not TPanel(Form1.Controls).Visible;
 
楼上兄弟说得没错:

我测了一下

procedure TForm1.Button1Click(Sender: TObject);
var
i: Integer;
begin

for i := 0 to Form1.ControlCount-1 do
if Form1.Controls is TSpeedButton then
TSpeedButton(Form1.Controls).Visible:= not TSpeedButton(Form1.Controls).Visible
else if Form1.Controls is TPanel then
TPanel(Form1.Controls).Visible:= not TPanel(Form1.Controls).Visible;
end;
 
帮顶,学习一下!
 
多谢了,是我搞晕了头脑.
 

Similar threads

S
回复
0
查看
916
SUNSTONE的Delphi笔记
S
S
回复
0
查看
894
SUNSTONE的Delphi笔记
S
后退
顶部