Board index » cppbuilder » "Control '' has no parent window" error when deriving from TWinControl

"Control '' has no parent window" error when deriving from TWinControl


2005-10-31 08:17:08 PM
cppbuilder76
Hi,
I'm deriving my class from TWinControl just to enable it to receive
windows messages.
class PACKAGE TMyClass : public TWinControl
{
public:
__fastcall virtual TMyClass (TComponent* AOwner) :
TWinControl(AOwner) { };
__fastcall TMyClass(HWND ParentWindow) : TWinControl(ParentWindow) {};
}
and in my main form i create it:
void __fastcall TForm1::btConnectClick(TObject *Sender)
{
myobj = new TMyClass(this);
// an exception 'Control '' has not parent window' is thrown at the
// line below. This is just a demonstration.
ShowMessage( IntToStr( myobj->Handle ) );
}
Did i forget to initialize something in the constructor?
Thanks in advance
Muzaffar
 
 

Re:"Control '' has no parent window" error when deriving from TWinControl

Muzaffar Mahkamov wrote:
Quote
void __fastcall TForm1::btConnectClick(TObject *Sender)
{
myobj = new TMyClass(this);
myobj->Parent = this;
Quote
Did i forget to initialize something in the constructor?
You could assign a parent there too.
Hans.
 

Re:"Control '' has no parent window" error when deriving from TWinControl

Hans Galema wrote:
Quote
Muzaffar Mahkamov wrote:

>void __fastcall TForm1::btConnectClick(TObject *Sender)
>{
>myobj = new TMyClass(this);

myobj->Parent = this;

>Did i forget to initialize something in the constructor?

You could assign a parent there too.

Hans.
Doing this raises an AV:
Project E:\dev\test12\test.exe faulted with message: 'access violation
at 0x7c90eddc: write of address 0x00030ee0'. Process Stopped. Use Step
or Run to continue.
 

{smallsort}

Re:"Control '' has no parent window" error when deriving from TWinControl

Muzaffar Mahkamov wrote:
Quote
>myobj->Parent = this;
>You could assign a parent there too.
Doing this raises an AV:
Which of the two suggestions did you implement.
Quote
Project E:\dev\test12\test.exe faulted with message: 'access violation
at 0x7c90eddc: write of address 0x00030ee0'. Process Stopped. Use Step
or Run to continue.
OK. So the Control has a Parent window now. That problem solved.
Please show minimal code that demonstrates the AV problem.
Hans.
 

Re:"Control '' has no parent window" error when deriving from TWinControl

Hans Galema wrote:
Quote
Muzaffar Mahkamov wrote:

OK. So the Control has a Parent window now. That problem solved.

Please show minimal code that demonstrates the AV problem.

Hans.
void __fastcall TForm1::btConnectClick(TObject *Sender)
{
myobj = new TMyClass(this);
myobj->Parent = this;
// an AV occurs in the line below
ShowMessage( IntToStr( myobj->Handle ) );
}
Did the same thing in Delphi and it works fine even without assigning
Parent manually.
 

Re:"Control '' has no parent window" error when deriving from TWinControl

Muzaffar Mahkamov wrote:
Quote
>Please show minimal code that demonstrates the AV problem.
void __fastcall TForm1::btConnectClick(TObject *Sender)
{
myobj = new TMyClass(this);

myobj->Parent = this;

// an AV occurs in the line below
ShowMessage( IntToStr( myobj->Handle ) );
}
That is not complete code. Where is the classdefinition?
With the one you supplied in your first post all goes well here (bcb5).
Hans.
 

Re:"Control '' has no parent window" error when deriving from TWinControl

Hans Galema wrote:
Quote
Muzaffar Mahkamov wrote:


That is not complete code. Where is the classdefinition?

With the one you supplied in your first post all goes well here (bcb5).

Hans.
I've included the class declaration in my first post. The definitions of
constructor/destructor are inlined. No other code is involved.
 

Re:"Control '' has no parent window" error when deriving from TWinControl

Muzaffar Mahkamov wrote:
Quote
I've included the class declaration in my first post. The definitions of
constructor/destructor are inlined. No other code is involved.
Destructor ? There is no destructor in the classdefinition you posted.
Please show complete code that causes the AV.
Hans.
 

Re:"Control '' has no parent window" error when deriving from TWinControl

Hans Galema wrote:
Quote
Muzaffar Mahkamov wrote:

>I've included the class declaration in my first post. The definitions
>of constructor/destructor are inlined. No other code is involved.

Destructor ? There is no destructor in the classdefinition you posted.

Please show complete code that causes the AV.

Hans.
I've already posted you the code that causes AV. The line with
ShowMessage( IntToStr( myobj->Handle ) );
causes the AV. Particularly, the call to myobj->Handle, which is not my
code. Even if i do:
int i = myob->Handle;
the application crashes with AV.
My configuration is: Borland C++ Builder 6 Enterprise, Update 4
 

Re:"Control '' has no parent window" error when deriving from TWinControl

Hans Galema wrote:
Quote
Muzaffar Mahkamov wrote:

>I've included the class declaration in my first post. The definitions
>of constructor/destructor are inlined. No other code is involved.

Destructor ? There is no destructor in the classdefinition you posted.

Please show complete code that causes the AV.

Hans.
Finally, i've found the problem. It was in the message handler. Sorry,
never thought that an incorrect declaration would cause an AV or Stack
overflow.
class PACKAGE TMyClass: public TWinControl
{
BEGIN_MESSAGE_MAP
VCL_MESSAGE_HANDLER(WM_DEVICE_CONNECTED, TMessage,
WMDEVICECONNECTED)
VCL_MESSAGE_HANDLER(WM_DEVICE_DISCONNECTED, TMessage,
WMDEVICEDISCONNECTED)
END_MESSAGE_MAP(TMyClass)
}
changed it to:
class PACKAGE TMyClass: public TWinControl
{
BEGIN_MESSAGE_MAP
VCL_MESSAGE_HANDLER(WM_DEVICE_CONNECTED, TMessage,
WMDEVICECONNECTED)
VCL_MESSAGE_HANDLER(WM_DEVICE_DISCONNECTED, TMessage,
WMDEVICEDISCONNECTED)
END_MESSAGE_MAP(TWinControl)
}
and it works fine.
Thanks for you help :)
 

Re:"Control '' has no parent window" error when deriving from TWinControl

Muzaffar Mahkamov wrote:
Quote
Finally, i've found the problem. It was in the message handler. Sorry,
never thought that an incorrect declaration would cause an AV or Stack
overflow.
Now there suddenly is a messagemap also in your class. Al least twice
you were asked to post all the code. And twice you stated that
class PACKAGE TMyClass : public TWinControl
{
public:
__fastcall virtual TMyClass (TComponent* AOwner) : TWinControl(AOwner) { };
__fastcall TMyClass(HWND ParentWindow) : TWinControl(ParentWindow) {};
}
was all there was.
If you need help then please be more exact.
Hans.