“控件”没有父窗口”错误 [英] "Control '' has no parent window" error
本文介绍了“控件”没有父窗口”错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我将控件放在窗体上时,出现此错误。
错误出现在这里:
I get this error when I drop my control on a form. The error appears here:
TAssociateFileExt = class(TGroupBox)
private
protected
public
btnAssociate : TButton;
constructor Create(aOwner: TComponent); override;
end;
constructor TAssociateFileExt.Create(aOwner: TComponent);
begin
inherited Create(aOwner);
Caption:= ''; <-------- error here
ClientHeight:= 125; <-------- and here
ClientWidth := 170;
DoubleBuffered:= TRUE;
btnAssociate:= TButton.Create(Self);
btnAssociate.Parent:= Self;
btnAssociate.Visible:= TRUE;
btnAssociate.Left:= 17;
btnAssociate.Top:= 26;
btnAssociate.Width:= 142;
btnAssociate.Height:= 25;
btnAssociate.Hint:= 'Associate this application with its files. When you double click a file this program will automatically start and load that file.';
btnAssociate.Caption:= 'Associate';
btnAssociate.DoubleBuffered:= TRUE;
btnAssociate.ParentDoubleBuffered:= FALSE;
btnAssociate.TabOrder:= 0;
btnAssociate.OnClick:= btnAssociateClick;
btnAssociateDel:= TButton.Create(Self);
btnAssociateDel.Parent:= Self;
btnAssociateDel.Visible:= TRUE;
btnAssociateDel.Left:= 17;
btnAssociateDel.Top:= 53;
btnAssociateDel.Width:= 142;
btnAssociateDel.Height:= 25;
btnAssociateDel.Hint:= 'Remove association';
btnAssociateDel.Caption:= 'Remove association';
btnAssociateDel.DoubleBuffered:= TRUE;
btnAssociateDel.ParentDoubleBuffered:= FALSE;
btnAssociateDel.TabOrder:= 1;
btnAssociateDel.OnClick:= btnAssociateDelClick;
chkAllUsers:= TCheckBox.Create(Self);
chkAllUsers.Parent:= Self;
chkAllUsers.Visible:= TRUE;
chkAllUsers.Left:= 31;
chkAllUsers.Top:= 97;
chkAllUsers.Width:= 115;
chkAllUsers.Height:= 17;
chkAllUsers.Hint:= 'Please note that if you want to do this for all users then you need administrator/elevated rights.';
chkAllUsers.Caption:= 'Do this for all users';
chkAllUsers.DoubleBuffered:= TRUE;
chkAllUsers.ParentDoubleBuffered:= FALSE;
chkAllUsers.TabOrder:= 2;
chkAllUsers.OnClick:= chkAllUsersClick;
end;
答案可能是字幕需要有效的窗口句柄。对?但是,David Intersimone(此处)的一篇文章说,可以在构造函数中设置Caption 。
Probably the answer is 'Caption needs a valid window handle'. Right? However, an article by David Intersimone (here) says it is ok to set Caption in the constructor.
- 文章有误吗?
- 我应该移动代码(Caption和TButton.Create)吗?在CreateWnd中(因为AfterConstruction不是一个好地方)?事实是,可以多次调用CreateWnd:第一次创建控件时或必须销毁并重新创建基础屏幕对象以反映属性更改时,会自动调用CreateWnd。
更新:
添加(aOwner:TComponent)后,作为J ...建议,在构造函数的声明中(在实现中),错误移至下一行(clientheight:= 90);
Update:
After adding (aOwner: TComponent), as J... suggested, to constructor's declaration (in Implementation) the error moved to the next line (clientheight:= 90);
推荐答案
我将代码移至CreateWindowHandle中。现在可以了。
完整代码:
I moved the code in CreateWindowHandle. Now it works. Full code:
UNIT cAssociateExt;
INTERFACE
USES
Windows, Messages, SysUtils, Classes, Controls, ExtCtrls, Forms, StdCtrls;
TYPE
TAssociateFileExt = class(TGroupBox)
private
protected
public
btnAssociate : TButton;
btnAssociateDel: TButton;
chkAllUsers : TCheckBox;
constructor Create(aOwner: TComponent); override;
procedure AfterConstruction; override;
procedure CreateWindowHandle(const Params: TCreateParams); override;
...
published
end;
procedure Register;
IMPLEMENTATION
procedure TAssociateFileExt.AfterConstruction;
begin
inherited; //Not a good place here
end;
procedure TAssociateFileExt.CreateWindowHandle(const Params: TCreateParams);
begin
inherited;
//DO NOT CREATE CONTROLS HERE! See: Sertac Akyuz's comment
Caption:= '';
ClientHeight:= 125;
ClientWidth := 170;
end;
constructor TAssociateFileExt.Create(aOwner: TComponent);
begin
inherited Create(aOwner);
DoubleBuffered:= TRUE;
btnAssociate:= TButton.Create(Self);
btnAssociate.Parent:= Self;
btnAssociate.Visible:= TRUE;
btnAssociate.Left:= 17;
...
end;
这篇关于“控件”没有父窗口”错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文