Invalid class typecast.???????

goldqq 2009-11-17 05:33:29
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Button5: TButton;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var
i: integer;
lblview: TLabel;
begin
for i := self.ControlCount-1 downto 0 do
begin
if (self.controls[i] is TButton) and
((self.controls[i] as TButton).tag = 0) then
begin
lblView := Tlabel.Create(Self);
lblView.parent := self;
lblView.tag := 1;
lblview.Height := (Self.controls[i] as TButton).height;
lblview.width := (self.controls[i] as TButton).width;
lblview.top := (self.controls[i] as TButton).top;
lblview.left := (self.controls[i] as TButton).left;
lblview.font := (self.controls[i] as TButton).font;
lblview.caption := (self.controls[i] as TButton).caption;
lblview.visible := true;
lblview.show;

(Self.Controls[i] as TButton).Visible := false;
end;
end;
end;

end.

请各位高手帮我看看 为什么报“Invalid class typecast”
...全文
762 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
弘石 2009-11-18
  • 打赏
  • 举报
回复
上面多了一句
mmotest.Lines.Clear;
是我原来的代码
弘石 2009-11-18
  • 打赏
  • 举报
回复

procedure TF_Main.btn2Click(Sender: TObject);
var
i: integer;
lblview: TLabel;
lstBtn: TList;
btn: TButton;
begin
mmotest.Lines.Clear;
lstBtn := TList.Create;
try
for i := ControlCount-1 downto 0 do
begin
if (controls[i] is TButton) and
((controls[i] as TButton).tag = 0) then
begin
lstBtn.Add( Controls[i] );
end;
end;
for i := 0 to lstBtn.Count-1 do
begin
btn := TButton(lstBtn[i]);
lblView := Tlabel.Create(Self);
lblView.parent := self;
lblView.tag := 1;
lblview.Height := btn.height;
lblview.width := btn.width;
lblview.top := btn.top;
lblview.left := btn.left;
lblview.font := btn.font;
lblview.caption := btn.caption;
lblview.visible := true;

btn.Visible := false;
end;
finally
lstBtn.Free;
end;
end;
ttbook001 2009-11-18
  • 打赏
  • 举报
回复
(self.controls[i] is TButton) and
((self.controls[i] as TButton).tag = 0) 这里是否有问题;
判断跟结果都在IF语句里面了,


beifangke 同学的比较标准
弘石 2009-11-18
  • 打赏
  • 举报
回复
创建label并设置其parent为当前窗体后,这个label就添加到窗体的controllist中,而根据事实,插入时并没有把label放到controllist的最后,所以你的程序虽然使用了downto,也是不行的

另外,不知道你在button上放一个label是干什么?是改变button的效果吗?
风之谷 2009-11-18
  • 打赏
  • 举报
回复
楼上说的对~
beifangke 2009-11-18
  • 打赏
  • 举报
回复
改成这样试试

procedure TForm1.FormCreate(Sender: TObject);
var
i: integer;
lblview: TLabel;
begin
for i := self.ControlCount-1 downto 0 do
begin
if (self.controls[i] is TButton) then
if ((self.controls[i] as TButton).tag = 0) then
begin
lblView := Tlabel.Create(Self);
lblView.parent := self;
lblView.tag := 1;
lblview.Height := (Self.controls[i] as TButton).height;
lblview.width := (self.controls[i] as TButton).width;
lblview.top := (self.controls[i] as TButton).top;
lblview.left := (self.controls[i] as TButton).left;
lblview.font := (self.controls[i] as TButton).font;
lblview.caption := (self.controls[i] as TButton).caption;
lblview.visible := true;
lblview.show;

(Self.Controls[i] as TButton).Visible := false;
end;
end;
end;


风之谷 2009-11-18
  • 打赏
  • 举报
回复
lblView.parent := self; self的controls可能有label ,label强制转换成button 出错
Harryfin 2009-11-18
  • 打赏
  • 举报
回复
说强制转型出错的没看到别人这句么“if self.controls[i] is TButton”

我感觉2楼正解
wooden954 2009-11-17
  • 打赏
  • 举报
回复
{$B-}的作用是当编译形如if (A and B) then的表达式时,程序先计算A是否为真,如果为真,则不再计算B的值。也就不会发生当self.controls[i]不是TButton的情况下再进行(self.controls[i] as TButton)这句强制转换语句了,不然肯定出错。

具体到你的代码,没有看出什么问题。
wooden954 2009-11-17
  • 打赏
  • 举报
回复
在{$R *.dfm} 语句后面加上一行{$B-}试试
de410 2009-11-17
  • 打赏
  • 举报
回复
self.controls[i] as TButton
类型强制转换造成的
goldqq 2009-11-17
  • 打赏
  • 举报
回复
为什么要创建的Label只有3个,应该是5个才对。因为有几个Button就创建几个Label
弘石 2009-11-17
  • 打赏
  • 举报
回复
因为你创建了label,delphi并没有把新创建的label插入列表最后
在lblView.parent := self; 之后,取得的self.controls[i]就不是button了
应该在创建之前把所有的button读到列表中,或者把所有的label创建完了再设置其parent属性

HeroicDragon 2009-11-17
  • 打赏
  • 举报
回复
没有问题啦,到底是提示哪行出错?

16,748

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 语言基础/算法/系统设计
社区管理员
  • 语言基础/算法/系统设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧