关于动态生成控件的问题,您能帮我改改程序的错吗?谢了(40分)

  • 主题发起人 主题发起人 陈君凯
  • 开始时间 开始时间

陈君凯

Unregistered / Unconfirmed
GUEST, unregistred user!
unit Unit2;

interface

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

type
TForm2 = class(TForm)
Button1: TButton;
Button2: TButton;
Panel1: TPanel;
Image1: TImage;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form2: TForm2;

implementation
uses unit1;
{$R *.DFM}

procedure TForm2.FormCreate(Sender: TObject);
var shaped:array of TShape;
i,j:integer;
q:nodeptr;
begin
for i:=1 to n do
begin
shaped:=TShape.Create(self);
shaped.Parent:=Self;
shaped.Height:=30;
shaped.Width:=30;
shaped.Top:=adj.zb.y1+15;
shaped.Left:=adj.zb.x1-15;
randomize;
shaped.Brush.Color:=rgb(random(255),random(255),random(255));
shaped.Brush.Style:=bsSolid;
randomize;
shaped.Pen.Color:=rgb(random(254),random(254),random(254));
shaped.Pen.Mode:=pmCopy;
shaped.Pen.Style:=psSolid;
shaped.Pen.Width:=1;
shaped.Shape:=stCircle;
Form2.Image1.Canvas.TextOut(adj.zb.x1-20,adj.zb.y1-40,Form1.Edit1.Text);
end;
new(q);
for i:=1 to n do
begin
q:=adj.link;
while q<>nil do
begin
Form2.Canvas.Pen.Color:=clOlive;
Form2.Canvas.Pen.Width:=1;
Form2.Canvas.MoveTo(adj.zb.x1+15,adj.zb.y1);
Form2.Canvas.LineTo(adj[q^.num].zb.x1-15,adj[q^.num].zb.y1);
q:=q^.link
end;
end;
i:=1;
repeat
j:=i+1;
Form2.Canvas.Pen.Color:=clPurple;
Form2.Canvas.Pen.Width:=2;
Form2.Canvas.MoveTo(adj[pre].zb.x1+15,adj[pre].zb.y1);
Form2.Canvas.LineTo(adj[pre[j]].zb.x1-15,adj[pre[j]].zb.y1);
i:=i+1;
until(j=n)
end;

end.
问题出在这一句:
shaped:=TShape.Create(self);
不知为何?望高人不吝赐教
 
把self改成form2试试
 
你的问题在这里:
  var shaped:array of TShape;
这里没有为该数组分配内存,所以后面的shaped:=TShape.Create(self)会
出错。
 
出错信息?
 
你必须在代码中用SetLength为shaped数组分配内存:
  SetLength(shaped, n); // n为数组成员数量
把上面这一句放在begin之后for之前。
 
还有一个错误:
问题在这一行:
shaped.Top:=adj.zb.y1+15;
提示信息为:
[Hint] Unit2.pas(54): Value assigned to 'q' never used。
源程序在上方,我不知道该怎么改?
能否告知?分数一定为您加上.
 
只是一个hint嘛! 不算错误.
 
Hint可以忽略不计的。
 
但是程序运行是老是出错,我把程序贴出来,能帮我改改错吗?
谢了
 
本程序分两个窗体,第二个的程序已写出来了,在上头。
第一个的源程序如下:
unit Unit1;

interface

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

type
TForm1 = class(TForm)
Button1: TButton;
Label1: TLabel;
Panel1: TPanel;
Edit1: TEdit;
Label2: TLabel;
Edit2: TEdit;
Label3: TLabel;
Edit3: TEdit;
Label4: TLabel;
Edit4: TEdit;
RadioButton1: TRadioButton;
RadioButton2: TRadioButton;
Button2: TButton;
Label5: TLabel;
Edit5: TEdit;
Label6: TLabel;
Edit6: TEdit;
Label7: TLabel;
Edit7: TEdit;
Label8: TLabel;
Edit8: TEdit;
Label9: TLabel;
Edit9: TEdit;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
type
nodeptr=^node;
node=RECORD
num:integer;
time1:integer;
link:nodeptr;
end;
zbnode=record
x1:integer;
y1:integer;
end;
node1=record
name:string;
count:integer;
zb:zbnode;
link:nodeptr;
end;
headnodes=ARRAY of node1;
nlist=ARRAY of integer;
var
Form1: TForm1;
adj:headnodes;
longest,pre,X,Y:nlist;
n:integer;
implementation
uses unit2;
{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var s1,i:integer;
s:string;
j:integer;
p,s2:nodeptr;
begin
while RadioButton2.Checked=False do
begin
i:=strtoint(Form1.Edit2.Text);
if RadioButton1.Checked=true then
begin
adj.name:=Form1.Edit1.Text;
s1:=trunc(int(strToDate(Form1.Edit5.Text))-int(strToDate(Form1.Edit4.Text)));
adj.zb.x1:=strtoint(Form1.Edit8.Text);
adj.zb.y1:=strtoint(Form1.Edit9.Text);
s:=Form1.Edit6.Text;
j:=strtoint(Form1.Edit7.Text);
adj[j].count:=adj[j].count+1;
p:=adj.link;;
while(p<>nil)and(p^.num<>j)do
p:=p^.link;
if p=nil then
begin
new(s2);
s2^.num:=j;
s2^.time1:=s1;
s2^.link:=nil;
p.link:=s2;
end;
Form1.Edit4.Text:='';
Form1.Edit5.Text:='';
Form1.Edit6.Text:='';
Form1.Edit7.Text:='';
end
else
begin
n:=n+1;
Form1.Edit1.Text:='';
Form1.Edit2.Text:='';
Form1.Edit3.Text:='';
Form1.Edit4.Text:='';
Form1.Edit5.Text:='';
Form1.Edit6.Text:='';
Form1.Edit7.Text:='';
Form1.Edit8.Text:='';
Form1.Edit9.Text:='';
end
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
hp,tp,i,k:integer;
p:nodeptr;
q:array of integer;
begin
if RadioButton2.Checked=true then
begin
for i:=1 to 18 do
longest:=0;
tp:=1;
q[tp]:=1;
hp:=1;
pre[1]:=0;
while hp<>tp do
begin
i:=q[hp];
hp:=hp+1;
p:=adj.link;
while p<>nil do
begin
k:=p^.num;
adj[k].count:=adj[k].count-1;
if longest+p^.time1>longest[k]
then begin
longest[k]:=longest[k]+p^.time1;
pre[k]:=i
end;
if adj[k].count=0
then begin
tp:=tp+1;
q[tp]:=k
end ;
p:=p^.link;
end
end;
Form2:=TForm2.Create(application);
Form2.show;
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
n:=1;
end;

end.
运行时提示出错,不只在那出错,请告知,谢了
 
我这样做没出问题:
procedure TForm1.FormCreate(Sender: TObject);
var
Shapes:array[0..100] of TShape;
i:Integer;
begin
for I:= 0 to 100 do
begin
Shapes:=TShape.Create(Self);
Shapes.Parent:=self;
Shapes.left:=i+10;
Shapes.Top:=100;
Shapes.width:=50;
Shapes.Height:=50;
end;
end;
 
多人接受答案了。
 
后退
顶部