1

I've checked out many threads of a similar title but they haven't helped.

The following compiles and installs to the component palette, but when I try to add the component to a panel, I get the error message mentioned in the thread title.

Could anyone please explain why?

__fastcall TEditBox::TEditBox(TComponent* Owner) : TGroupBox(Owner)
{
    ToolBar=new TToolBar(this);
    ToolBar->Parent=this;
    TToolButton *Btn=new TToolButton(ToolBar);
    Btn->Parent=ToolBar;
}

If I omit the Btn->Parent=ToolBar line, everything's OK, so presumably that's the problem line?

Remy Lebeau
  • 505,946
  • 29
  • 409
  • 696
NoComprende
  • 641
  • 4
  • 14

1 Answers1

1

Assigning a ToolButton's Parent requires the ToolBar to have an allocated HWND, which requires it to have a Parent with an allocated HWND, and so on. But your EditBox does not have a Parent (or a Name) assigned yet when its constructor is called, so the ToolBar cannot allocate an HWND yet, hence the error.

If you want your Toolbar to have a default button at runtime, you need to move the creation of the button to the EditBox's virtual Loaded() method (or even the SetParent() method), eg:

__fastcall TEditBox::TEditBox(TComponent* Owner)
    : TGroupBox(Owner)
{
    ToolBar=new TToolBar(this);
    ToolBar->Parent=this;
}

void __fastcall TEditBox::Loaded()
{
    TGroupBox::Loaded();
    TToolButton *Btn=new TToolButton(ToolBar);
    Btn->Parent=ToolBar;
}
Remy Lebeau
  • 505,946
  • 29
  • 409
  • 696