Solved

My Owner.......

Posted on 2001-07-19
8
339 Views
Last Modified: 2013-11-23
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.CreateFmt('Incorrect 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(AOwner: 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,TBaseChessPanel]);
end;

end.

0
Comment
Question by:Yunoshev
  • 5
  • 3
8 Comments
 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
are you sure you mean the owner and not the parent?
0
 
LVL 17

Expert Comment

by:geobul
Comment Utility
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.ClassName);
  btn.Free;
end;

Regards, Geo
0
 
LVL 17

Accepted Solution

by:
geobul earned 50 total points
Comment Utility
Well, here is the example: TMyButton accepts TPanel only as a Parent, e.g. cannot be placed on a form.

TMyButton = class(TButton)
  protected
    procedure MySetParent(AParent: TWinControl);
  public
    Constructor Create(AOwner: TComponent); override;
    property Parent write MySetParent;
end;

Constructor TMyButton.Create(AOwner: TComponent);
begin
 inherited Create(AOwner);
end;

procedure TMyButton.MySetParent(AParent: TWinControl);
begin
  If  not (AParent is TPanel) Then
  Begin
    MessageDlg('Incorrect Parent',mtError,[mbOk],0);
    Free;
    raise EInvalidOperation.CreateFmt('Incorrect Owner %s',[AParent.Name]);
  End else SetParent(AParent);
end;

Regards, Geo
0
 
LVL 17

Expert Comment

by:geobul
Comment Utility
Correction:
TMyButton accepts TPanel *AND ITS DESCENDANTS* only ...
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 17

Expert Comment

by:geobul
Comment Utility
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('TPanel') Then

Regards, Geo
0
 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
good work, geo ;-)
0
 
LVL 17

Expert Comment

by:geobul
Comment Utility
Thanks Meikl. But Yunoshev wasn't very generous. He made my profile look ugly (ha-ha) ;-)
0
 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
oh, i see, only a c-grade :-(
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now