简单问题 来帮忙呀!!!!!

shuzhan87 2009-02-05 07:11:43
我定义了一个类 在窗口中建立的时候不能显示出来 不知道如何 单击按钮建立了类的对象 但类中的label和image不能在窗口中显示出来 高手帮忙
my2=class(TWinControl)
private
tex: Tlabel;
pic: Timage;
public
procedure init(AOwner: TComponent; AParent: TWinControl);
end;

implementation
procedure my2.init;
begin
tex:=Tlabel.Create(AOwner);
tex.Parent:=AParent;
tex.Caption:='hehe';
tex.Visible:=true;
pic:=Timage.Create(AOwner);
pic.Picture.LoadFromFile('logo.jpg');
pic.Parent:=AParent;
pic.Visible:=true;
end;

窗口中我是这样使用这个类的
sz: array of my2;
implementation

{$R *.dfm}

procedure TForm1.BitBtn1Click(Sender: TObject);
var
lable: my2;
begin
i := i + 1;
SetLength(sz,i);
lable := my2.Create(self);
lable.Parent:=form1;
lable.Top:=i*10;
lable.Left:=i*6;
sz[i-1] := lable;
label1.Caption:=inttostr(i);
end;
...全文
79 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
7年 2009-02-06
  • 打赏
  • 举报
回复
你定义的过程你都没用,看到了吗?
starluck 2009-02-06
  • 打赏
  • 举报
回复
只是隨便改了下,訪問LABLE的TOP的屬性的話,按你的意思是訪問lable.lable 或lable.pic 的,這點要分清。
starluck 2009-02-06
  • 打赏
  • 举报
回复


unit Unit1;

interface

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

type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

my2=class(TWinControl)
private
tex: Tlabel;
pic: Timage;
public
constructor create(AOwner: TComponent; AParent: TWinControl);
//procedure Create(aOwner : Tcomponent);
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

{ my2 }

constructor my2.create(AOwner: TComponent; AParent: TWinControl);
begin
tex:=Tlabel.Create(AOwner);
tex.Parent:=AParent;
tex.Caption:='hehe';
tex.Visible:=true;
pic:=Timage.Create(AOwner);
//pic.Picture.LoadFromFile('logo.jpg');
pic.Parent:=AParent;
pic.Visible:=true;
end;



procedure TForm1.Button1Click(Sender: TObject);
var
lable: my2;
i : integer;
begin
i := i + 1;
// SetLength(sz,i);
lable := my2.create(Self,Form1);

// lable.Top:=i*10;
// lable.Left:=i*6;
// sz[i-1] := lable;
// label1.Caption:=inttostr(i);

end;

end.

bdmh 2009-02-06
  • 打赏
  • 举报
回复
lable := my2.Create(self); 并没有触发procedure init(AOwner: TComponent; AParent: TWinControl); 你的控件怎么会创建呢

在你的程序中调用lable.init(nil,lable.Parent);

或者在my2类中写构造函数create,把init方法放到create中
l_q_z 2009-02-05
  • 打赏
  • 举报
回复
在lable.Parent:=form1;后加一句
lable.init(nil,lable.Parent);
willflyz 2009-02-05
  • 打赏
  • 举报
回复
不是高手,不过觉得写得很乱..
建议还是从基础的学起吧.

5,939

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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