Solved

My Owner.......

Posted on 2001-07-19
8
344 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 27

Expert Comment

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

Expert Comment

by:geobul
ID: 6298952
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
ID: 6299226
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 17

Expert Comment

by:geobul
ID: 6299279
Correction:
TMyButton accepts TPanel *AND ITS DESCENDANTS* only ...
0
 
LVL 17

Expert Comment

by:geobul
ID: 6299309
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
ID: 6301848
good work, geo ;-)
0
 
LVL 17

Expert Comment

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

Expert Comment

by:kretzschmar
ID: 6301889
oh, i see, only a c-grade :-(
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

729 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