Solved

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

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

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!

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses
Course of the Month6 days, 11 hours left to enroll

636 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