Solved

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

Posted on 2001-08-07
4
255 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
[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
  • 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

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
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…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

734 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