Solved

"control '' has no parent"  error...

Posted on 2001-08-07
4
240 Views
Last Modified: 2010-04-04
Hi,
  I'm writing a visual component based on TPaintBox.
but the create method raise the error :"control '' has no parent", I don't understant why. Can you help me ?

  I reduced the code to the max in order to keep the error :

unit PaintBox1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ExtCtrls;

type
  TPaintBox1 = class(TPaintBox)
  private
  protected
  public
    constructor create(AOwner:Tcomponent);Override;
  published
  end;

procedure Register;
implementation

constructor TPaintBox1.Create(AOwner:Tcomponent);
var R:TRect;
begin
  inherited;
  R.Top:=5;
  R.Left:=5;
  R.Bottom:=Height-5;
  R.Right:=width-5;
  Self.Canvas.Brush.color:=clLime;
  Self.Canvas.FillRect(R);
end;

procedure Register;
begin
  RegisterComponents('Additional', [TPaintBox1]);
end;

end.

Thanks for help.

Mike
0
Comment
Question by:gklux
  • 2
4 Comments
 
LVL 11

Expert Comment

by:robert_marquardt
ID: 6358750
Try
  inherited Create(AOwner);
0
 

Author Comment

by:gklux
ID: 6358760
Sorry, the full error message is :
"control '' has no parent window"

Mike
0
 
LVL 20

Accepted Solution

by:
Madshi earned 100 total points
ID: 6358821
constructor TPaintBox1.Create(AOwner:Tcomponent);
var R:TRect;
begin
 inherited;
 R.Top:=5;
 R.Left:=5;
 R.Bottom:=Height-5;
 R.Right:=width-5;

This is alright. But this...

 Self.Canvas.Brush.color:=clLime;
 Self.Canvas.FillRect(R);

... fails, because in the moment where you access the Canvas property, a handle to the paintbox is needed. But a handle can only be created if the parent window of the paint box is known. But the parent is known later, not in the constructor.
So I recommend to move the canvas stuff to a "later location". Maybe you can overwrite the "SetParent" method and do it there, after the parent was set to a non-nil value.

Regards, Madshi.
0
 

Author Comment

by:gklux
ID: 6359270
Very helpfull answer Madshi, has always  :-)

regards

Mike
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

744 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

14 Experts available now in Live!

Get 1:1 Help Now