一个让我百思不得其解的问题(100分)

D

dmg01

Unregistered / Unconfirmed
GUEST, unregistred user!
我写了一个简单的控件,全部代码如下:
unit Unit1;

interface

uses
Controls, DBCtrls, Classes, Graphics;

type

TMyDBLookUpComboBox = class(TWinControl)
private
{ Private declarations }
FDBLookUpComboBox:TDBLookUpComboBox;
protected
{ Protected declarations }
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
published
{ Published declarations }
end;

procedure Register;

implementation

{ TMyDBLookUpComboBox }

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

FDBLookUpComboBox:=TDBLookUpComboBox.Create(Self);
FDBLookUpComboBox.Parent:=Self;
FDbLookupComboBox.Font.Name:='宋体';
FDbLookupComboBox.Font.Size:=9;
end;

procedure Register;
begin
RegisterComponents('ChenComm', [TMyDBLookUpComboBox]);
end;

end.

编译通过,安装也没有问题,在使用时当我从控件面板中选择该控件并将它放入一个Form中时系统报“Control '' has no parent window”错误,不知为什么?
 

FDBLookUpComboBox:=TDBLookUpComboBox.Create(Self);
FDBLookUpComboBox.Parent:=Self;
改成
FDBLookUpComboBox:=TDBLookUpComboBox.Create(AOwner);
FDBLookUpComboBox.Parent:=AOwner;
试试
 
// Maybe :

unit Unit1;

interface

uses
Controls, DBCtrls, Classes, Graphics;

type

TMyDBLookUpComboBox = class(TDBLookUpComboBox)
private
{ Private declarations }
protected
{ Protected declarations }
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
published
{ Published declarations }
end;

procedure Register;

implementation

{ TMyDBLookUpComboBox }

constructor TMyDBLookUpComboBox.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
Parent:=Self;
Font.Name:='宋体';
Font.Size:=9;
end;

procedure Register;
begin
RegisterComponents('ChenComm', [TMyDBLookUpComboBox]);
end;

end.

 
呵呵,自己的拥有者是自己,自己的父亲是自已,当然不行了。
 
maming大侠的方法固然可行,但和我的本意有出入,我的意思是把FDBLookupComboBox限定在控件的范围内,所以FDBLookupComboBox的Parent应该是Self啊!
 
还说一句,去掉Create中的FDBLookupComboBox的字体设置,该控件工作完全正常!
 
把TWinControl改为TCustomControl
 
我最初就是用的TCustomControl,有错误才换成TWinControl的。
 
FDBLookUpComboBox.Parent:=AOwner;
编译会出现如下错误:
[Error] UMyDBLookUpComoBox.pas(34): Incompatible types: 'TWinControl' and 'TComponent'
 
楼上兄弟:
FDBLookUpComboBox.Parent:=AOwner应为:
FDBLookUpComboBox.Parent:=TWincontrol(AOwner)
 
修改Create事件代码:
constructor TMyDBLookUpComboBox.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
Parent:=TWinControl(AOwner);

FDBLookUpComboBox:=TDBLookUpComboBox.Create(Self);
FDBLookUpComboBox.Parent:=Self;
FDbLookupComboBox.Font.Name:='宋体';
FDbLookupComboBox.Font.Size:=9;
end;

粗体为新增部分
 
问题解决了,谢谢各位!
 
顶部