When we create a component as a custom control and the control is dropped on a panel the control always appears on the form rather than the containing control. How do you set the parent of the custom control in Create so that when the button is dropped on panel the buttons parent is the panel?
TGlassButton = class(TCustomControl)
...
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
...
constructor TGlassButton.Create(AOwner: TComponent);
begin
inherited; ???????????
inherited Create(AOwner); ????????????
Parent := TWinControl( AComponent ); ??????????????
...
end;
Run Code Online (Sandbox Code Playgroud)
The problem is designtime creation not runtime. This works perfectly:
procedure TForm10.FormCreate(Sender: TObject);
begin
GlassButton0 := TGlassButton.Create( Panel1 );
GlassButton0.Parent := Panel1;
GlassButton0.Left := 20;
GlassButton0.Top := 6;
GlassButton0.Width := 150;
GlassButton0.Height := 25;
GlassButton0.Caption := 'Created At RunTime';
end;
Run Code Online (Sandbox Code Playgroud)
不要在构造函数中设置Parent属性!正如其他人所说,IDE和DFM流系统将在构造函数退出后自动分配父对象。如果需要在构造函数中执行依赖于已分配父项的操作,则需要重新设计组件。覆盖虚拟SetParent()和/或Loaded()方法,然后从那里进行操作。并if (csDesigning in ComponentState) then ...在可以避免设计时实际不需要的操作的地方使用检查。
| 归档时间: |
|
| 查看次数: |
3458 次 |
| 最近记录: |