急啊,钱不多,帮个忙,程序出错了. (10分)

  • 主题发起人 主题发起人 lzh1998
  • 开始时间 开始时间
L

lzh1998

Unregistered / Unconfirmed
GUEST, unregistred user!
如下执行第二次(当i=1)程序出错:(project proshuru.exe raised exception class Einvalidcast with message'Invalid class typecast'.
process stopped.Use step or run to continue )
var i,j:integer;
ttxt:TLabel;
text:tedit;
begin
i:=0;
j:=0;
while Form1.dbgrideh1.columns.Count>j do
begin
ttxt:=tlabel.Create(self);
ttxt.Parent:=ScrollBox1;
(form2.ScrollBox1.Controls as tlabel).Name:='ttxt'+inttostr(j);
(form2.ScrollBox1.Controls as tlabel).Caption:=Form1.dbgrideh1.columnS[j].Title.Caption+':';
(form2.ScrollBox1.Controls as tlabel).Left:=40;
(form2.ScrollBox1.Controls as tlabel).Top:=i*20;
i:=i+1;
text:=tedit.Create(self);
text.Parent:=ScrollBox1;
(form2.ScrollBox1.Controls as tedit).Name:='text'+inttostr(j);
(form2.ScrollBox1.Controls as tedit).Left:=100+40;
(form2.ScrollBox1.Controls as tedit).Top:=j*20;
i:=i+1;
j:=j+1;
end;
如果把如下删除掉就不会错了 为什么,上面的代码怎么救
text:=tedit.Create(self);
text.Parent:=ScrollBox1;
(form2.ScrollBox1.Controls as tedit).Name:='text'+inttostr(j);
(form2.ScrollBox1.Controls as tedit).Left:=100+40;
(form2.ScrollBox1.Controls as tedit).Top:=j*20;
i:=i+1;
 
一个控件,建了n次。
若多个,声明为控件数组
 
(form2.ScrollBox1.Controls as tedit) 到底是那个,也不直观,很容易出错的
是否新建的text,是的话,直接text.name:=...不就得了?
 
你需要记住 Controls 和 Components 的区别, Label 应该不是一个Control, 所以你在将
form2.ScrollBox1.Controls as tedit). 强制转换的时候出错,
一生中最爱说得很正确,直接付值是一个简单可行的方法。
不要乱用 Controls 和 Components, 除非你需要查询元件,或者你要遍历所有元件。
如果你想这样做可以参照如下程序察看你要使用的类名就知道错在那里了:

var i,j:integer;
ttxt:TLabel;
text:tedit;
begin
i:=0;
j:=0;
while Form1.dbgrideh1.columns.Count>j do
begin
ttxt:=tlabel.Create(self);
ttxt.Parent:=ScrollBox1;
showmessage(Form1.ScrollBox1.Controls.ClassName);
//以下注释掉
{(form2.ScrollBox1.Controls as tlabel).Name:='ttxt'+inttostr(j);
(form2.ScrollBox1.Controls as tlabel).Caption:=Form1.dbgrideh1.columnS[j].Title.Caption+':';
(form2.ScrollBox1.Controls as tlabel).Left:=40;
(form2.ScrollBox1.Controls as tlabel).Top:=i*20;}
i:=i+1;
text:=tedit.Create(self);
text.Parent:=ScrollBox1;
{(form2.ScrollBox1.Controls as tedit).Name:='text'+inttostr(j);
(form2.ScrollBox1.Controls as tedit).Left:=100+40;
(form2.ScrollBox1.Controls as tedit).Top:=j*20;}
i:=i+1;
j:=j+1;
end;

 
Control 一定要有焦点
 
要不要知道答案;

可惜分太少了
哈哈

告訴你;

var i,j:integer;
ttxt:TLabel;
text:tedit;
begin
i:=0;
j:=0;
while Form1.dbgrideh1.columns.Count>j do
begin
ttxt:=tlabel.Create(self);
ttxt.Parent:=ScrollBox1;
(form2.ScrollBox1.Controls as tlabel).Name:='ttxt'+inttostr(j);
(form2.ScrollBox1.Controls as tlabel).Caption:=Form1.dbgrideh1.columnS[j].Title.Caption+':';
(form2.ScrollBox1.Controls as tlabel).Left:=40;
(form2.ScrollBox1.Controls as tlabel).Top:=i*20;
i:=i+1;
text:=tedit.Create(self);
text.Parent:=ScrollBox1; [blue]//i 已經加了 1, 所以當你第二次執行時 下面的轉換當然就錯了!!!哈哈[/blue]
[red](form2.ScrollBox1.Controls as tedit).Name:='text'+inttostr(j);
(form2.ScrollBox1.Controls as tedit).Left:=100+40;
(form2.ScrollBox1.Controls as tedit).Top:=j*20;[/red]
i:=i+1;
j:=j+1;
end;

 
用数组!

var i,j:integer;
ttxt:array[0..20] of TLabel;/////
text:array[0..20] of tedit;//////////
begin
i:=0;
j:=0;
while Form1.dbgrideh1.columns.Count>j do
begin
ttxt[j]:=tlabel.Create(self);
ttxt[j].Parent:=ScrollBox1;
showmessage(Form1.ScrollBox1.Controls.ClassName);
//以下注释掉
{(form2.ScrollBox1.Controls as tlabel).Name:='ttxt'+inttostr(j);
(form2.ScrollBox1.Controls as tlabel).Caption:=Form1.dbgrideh1.columnS[j].Title.Caption+':';
(form2.ScrollBox1.Controls as tlabel).Left:=40;
(form2.ScrollBox1.Controls as tlabel).Top:=i*20;}
i:=i+1;
text[j]:=tedit.Create(self);
text[j].Parent:=ScrollBox1;
{(form2.ScrollBox1.Controls as tedit).Name:='text'+inttostr(j);
(form2.ScrollBox1.Controls as tedit).Left:=100+40;
(form2.ScrollBox1.Controls as tedit).Top:=j*20;}
i:=i+1;
j:=j+1;
end;
 
最后我自己用如下代码完成了, jsxjd的方法也是对的,更好控制组件..谢了

var i,j:integer;
ttxt:TLabel;
text:tedit;
begin
rr:=strtoint(form1.edit1.Text);
i:=0;
j:=0;
while Form1.dbgrideh1.columns.Count>j do
begin
ttxt:=nil;
text:=nil;
ttxt:=tlabel.Create(self);
ttxt.Parent:=ScrollBox1;
ttxt.Caption:=Form1.dbgrideh1.columnS[j].Title.Caption+':';
ttxt.Left:=40;
ttxt.Top:=i*25+5;
ttxt.Name:='ttxt'+inttostr(j);
text:=tedit.Create(self);
text.Parent:=ScrollBox1;
text.Left:=100+40;
text.Top:=i*25;
text.Name:='text'+inttostr(j);
i:=i+1;
j:=j+1;
end;
end;
 

Similar threads

后退
顶部