有关一控件不能显示问题(50分)

  • 主题发起人 主题发起人 骆璇
  • 开始时间 开始时间

骆璇

Unregistered / Unconfirmed
GUEST, unregistred user!
unit mybtn;

interface
uses
Windows, Messages, SysUtils, Classes, Controls, ExtCtrls,
buttons,stdctrls;

type
Tmyedit= class(TWinControl)
private
FMyBtn:TSpeedButton;
vmybtn:tspeedbutton;
FMyEdit:TEdit;
vmyedit:tedit;
public
constructor Create(AOwner:TComponent);override;
destructor destroy;override;
published
property Button:TSpeedButton read Fmybtn;
property Edit:TEdit read Fmyedit;
end;

procedure Register;


implementation

procedure Register;
begin
RegisterComponents('samples', [Tmyedit]);
end;

constructor TMyEdit.Create(AOwner:TComponent);
begin
inherited ;
FMyBtn:=TSpeedButton.Create(nil);
FMyedit:=TEdit.Create(nil);
FMyEdit.Left:=0;
FMyEdit.Top:=0;
FMyEdit.Height:=30;
FMyEdit.Width:=150;
FMyBtn.Left :=151;
FMyBtn.Top:=0;
FMyBtn.Height:=30;
FMyBtn.Width:=30;
end;
destructor Tmyedit.destroy;
begin
fmybtn.Free;
fmyedit.Free;
end;

end.

运行正确,但为何不能显示button和edit?
 
FMyBtn.Parent:=self;
FMyEdit.Parent:=self;

 
FMyEdit.parent=Form1, fmybtn.parent=Form1?
 
inherited Create(AOwner);
FMyBtn:=TSpeedButton.Create(Self);
FMyBtn.Parent := Self;
FMyedit:=TEdit.Create(Self);
FMyEdit.Parent := Self;
 
可以了,但为何每次调用控件,在form加上这个控件时,如果想不用删掉或剪切时总报错,
如果用想关闭所有打开的文件时,也报错。
错误为“access violation at address 4001doa2 in module 'rtl60.bpl',read of address 00000001”
有时报其它的*.bpl错误,那应该如何处理,怎样做到可跟其它控件那样可cut掉的。
 
应该是访问了空指针
你可以跟踪组件 Destroy过程
看看在那儿引用了空指针
 
destroy中也需要inherited的。
 
加了啦,不过一cut时就退出整个delphi啦,怎办?
 
destructor Tmyedit.destroy;
begin
fmybtn.parent:=nil;
fmybtn.Free;
fmyedit.parent:=nil;
fmyedit.Free;

inherited;
end;
 
你说不行,让我又试了一次,谁说不行?
inherited需要放在destroy的末尾而不是开始。

下面的代码通过:
unit mybtn;

interface
uses
Windows, Messages, SysUtils, Classes, Controls, ExtCtrls,
buttons, stdctrls;

type
Tmyedit = class(TWinControl)
private
FMyBtn: TSpeedButton;
// vmybtn: tspeedbutton;
FMyEdit: TEdit;
// vmyedit: tedit;
public
constructor Create(AOwner: TComponent); override;
destructor destroy; override;
published
property Button: TSpeedButton read Fmybtn;
property Edit: TEdit read Fmyedit;
end;

procedure Register;


implementation

procedure Register;
begin
RegisterComponents('MyControl', [Tmyedit]);
end;

constructor TMyEdit.Create(AOwner: TComponent);
begin
inherited;

FMyBtn := TSpeedButton.Create(nil);
FMyedit := TEdit.Create(nil);

FMyEdit.Left := 0;
FMyEdit.Top := 0;
FMyEdit.Height := 30;
FMyEdit.Width := 150;
FMyBtn.Left := 151;
FMyBtn.Top := 0;
FMyBtn.Height := 30;
FMyBtn.Width := 30;
end;

destructor Tmyedit.destroy;
begin
fmybtn.Free;
fmyedit.Free;

inherited;
end;

end.
 
多人接受答案了。
 
后退
顶部