Yunoshev
asked on
My Owner.......
I'm writing a visual component (see below) and I want it to be created only on another my component....
Can I do the such thing: if I'm placing my component on the another my component - the second component will became an owner of the first component?
unit ChessPanel;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls;
type
TChessPanel = class(TCustomPanel)
private
{ Private declarations }
protected
{ Protected declarations }
public
{ Public declarations }
Constructor Create(AOwner: TComponent); override;
published
{ Published declarations }
Property Align;
end;
TBaseChessPanel = class(TCustomPanel)
private
{ Private declarations }
protected
{ Protected declarations }
public
{ Public declarations }
Constructor Create(AOwner: TComponent); override;
published
{ Published declarations }
Property Align;
end;
procedure Register;
implementation
Constructor TChessPanel.Create(AOwner: TComponent);
begin
If not (AOwner is TBaseChessPanel) Then
Begin
MessageDlg('Incorrect Owner',mtError,[mbOk],0);
raise EInvalidOperation.CreateFm t('Incorre ct Owner %s',[AOwner.Name]);
End;
inherited Create(AOwner);
ControlStyle := [csAcceptsControls, csCaptureMouse, csClickEvents,
csSetCaption, csOpaque, csDoubleClicks, csReplicatable];
Width := 185;
Height := 41;
BevelOuter := bvNone;
BevelWidth := 1;
Color := clBtnFace;
UseDockManager := True;
end;
Constructor TBaseChessPanel.Create(AOw ner: TComponent);
begin
inherited Create(AOwner);
ControlStyle := [csAcceptsControls, csCaptureMouse, csClickEvents,
csSetCaption, csOpaque, csDoubleClicks, csReplicatable];
Width := 185;
Height := 41;
BevelOuter := bvNone;
BevelWidth := 1;
Color := clBtnFace;
UseDockManager := True;
end;
procedure Register;
begin
RegisterComponents('Chess' , [TChessPanel,TBaseChessPan el]);
end;
end.
Can I do the such thing: if I'm placing my component on the another my component - the second component will became an owner of the first component?
unit ChessPanel;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls;
type
TChessPanel = class(TCustomPanel)
private
{ Private declarations }
protected
{ Protected declarations }
public
{ Public declarations }
Constructor Create(AOwner: TComponent); override;
published
{ Published declarations }
Property Align;
end;
TBaseChessPanel = class(TCustomPanel)
private
{ Private declarations }
protected
{ Protected declarations }
public
{ Public declarations }
Constructor Create(AOwner: TComponent); override;
published
{ Published declarations }
Property Align;
end;
procedure Register;
implementation
Constructor TChessPanel.Create(AOwner:
begin
If not (AOwner is TBaseChessPanel) Then
Begin
MessageDlg('Incorrect Owner',mtError,[mbOk],0);
raise EInvalidOperation.CreateFm
End;
inherited Create(AOwner);
ControlStyle := [csAcceptsControls, csCaptureMouse, csClickEvents,
csSetCaption, csOpaque, csDoubleClicks, csReplicatable];
Width := 185;
Height := 41;
BevelOuter := bvNone;
BevelWidth := 1;
Color := clBtnFace;
UseDockManager := True;
end;
Constructor TBaseChessPanel.Create(AOw
begin
inherited Create(AOwner);
ControlStyle := [csAcceptsControls, csCaptureMouse, csClickEvents,
csSetCaption, csOpaque, csDoubleClicks, csReplicatable];
Width := 185;
Height := 41;
BevelOuter := bvNone;
BevelWidth := 1;
Color := clBtnFace;
UseDockManager := True;
end;
procedure Register;
begin
RegisterComponents('Chess'
end;
end.
are you sure you mean the owner and not the parent?
Hi,
I think that when you place a component somewhere on a form in design mode, the form always becomes the owner of that component. This is necessary for destroying the form and all its components correctly and I don't know a way to avoid this.
On the other hand, when creating components in run-time, you can use TPanel for example.
procedure TForm1.Button1Click(Sender : TObject);
var
btn: TButton;
begin
btn := TButton.Create(Panel1);
btn.Parent := Panel1;
btn.Caption := 'Hello';
ShowMessage(btn.Owner.Clas sName);
btn.Free;
end;
Regards, Geo
I think that when you place a component somewhere on a form in design mode, the form always becomes the owner of that component. This is necessary for destroying the form and all its components correctly and I don't know a way to avoid this.
On the other hand, when creating components in run-time, you can use TPanel for example.
procedure TForm1.Button1Click(Sender
var
btn: TButton;
begin
btn := TButton.Create(Panel1);
btn.Parent := Panel1;
btn.Caption := 'Hello';
ShowMessage(btn.Owner.Clas
btn.Free;
end;
Regards, Geo
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Correction:
TMyButton accepts TPanel *AND ITS DESCENDANTS* only ...
TMyButton accepts TPanel *AND ITS DESCENDANTS* only ...
One last thing: if you want to prevent the parent's descendants to accept TMyButton then replace
If not (AParent is TPanel) Then
with
If not AParent.ClassNameIs('TPane l') Then
Regards, Geo
If not (AParent is TPanel) Then
with
If not AParent.ClassNameIs('TPane
Regards, Geo
good work, geo ;-)
Thanks Meikl. But Yunoshev wasn't very generous. He made my profile look ugly (ha-ha) ;-)
oh, i see, only a c-grade :-(