Solved

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

Posted on 2001-08-07
4
249 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

840 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