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
  • 555,201
  • 31
  • 458
  • 770
NoComprende
  • 731
  • 4
  • 14
  • Don't think you need to assign parent for a tool button. Doesn't the toolbar do that. – David Heffernan Aug 13 '16 at 12:40
  • Tried it David but then no button shows. Only way I could get the button to show was to assign the Btn->Parent after the constructor had executed. – NoComprende Aug 13 '16 at 13:15
  • also see: https://stackoverflow.com/questions/23316113/control-has-no-parent-window-error – Gabriel Jul 02 '19 at 06:51
  • Possible duplicate of ["Control '' has no parent window" error](https://stackoverflow.com/questions/23316113/control-has-no-parent-window-error) – Gabriel Jul 02 '19 at 06:53

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
  • 555,201
  • 31
  • 458
  • 770