Board index » cppbuilder » TTreeNodes->Add before TCustomTreeView->Parent set

TTreeNodes->Add before TCustomTreeView->Parent set

I need to dynamically populate a TCustomTreeView descendant class before it
is displayed on the form.

I'm trying to pre-populate a TCustomTreeView descendant class in the
constructor. When I do this, the TCustomTreeView->Items->Add(Null, Str) line
throws an exception:

EInvalidOperation - 'Control '' has no parent window'

These items are created dynamically based on system information.

On the Create event for the form, I have the following:

    MyTree=new TMyTreeView(Form1);
    MyTree->Align=alClient;
    MyTree->Parent=Form1;

Any ideas would be greatly appreciated.

Thanks,

Jason Dentler

 

Re:TTreeNodes->Add before TCustomTreeView->Parent set


As the exception is telling you, you must assign the TreeView's Parent
before you can add items to it. Also, don't use OnCreate, it's unstable in
C++.  Use the form's constructor instead.

If you're trying to populate the items from inside the TreeView's
constructor, that won't work, as the Parent hasn't been set when the
constructor is executing.  What you can do, though, is add another method to
your form that populates the items, ie:

    __fastcall TForm1::TForm1(TComponet *Owner)
        : TForm(Owner)
    {
        MyTree = new TMyTreeView(this);
        MyTree->Parent = this;
        MyTree->Align = alClient;
        PopulateDefaults();
    }

    void __fastcall TForm1::PopulateDefaults(void)
    {
        MyTree->Items->Clear();
        for(int x = 0; x < however many you need; x++)
            MyTree->Items->Add(NULL, StringToAdd);
    }

Gambit

Quote
"Anonymous" <anonym...@asdf.com> wrote in message news:3bc89f80_2@dnews...
> I'm trying to pre-populate a TCustomTreeView descendant class in the
> constructor. When I do this, the TCustomTreeView->Items->Add(Null, Str)
line
> throws an exception:

> EInvalidOperation - 'Control '' has no parent window'

> These items are created dynamically based on system information.

> On the Create event for the form, I have the following:

>     MyTree=new TMyTreeView(Form1);
>     MyTree->Align=alClient;
>     MyTree->Parent=Form1;

> Any ideas would be greatly appreciated.

> Thanks,

> Jason Dentler

Re:TTreeNodes->Add before TCustomTreeView->Parent set


I'm writing a completely self-contained component. I need to be able to
automatically pre-populate this tree view with items some time after it has
a parent, but before it is displayed. Any ideas?

Quote
"Anonymous" <anonym...@asdf.com> wrote in message news:3bc89f80_2@dnews...
> I need to dynamically populate a TCustomTreeView descendant class before
it
> is displayed on the form.

> I'm trying to pre-populate a TCustomTreeView descendant class in the
> constructor. When I do this, the TCustomTreeView->Items->Add(Null, Str)
line
> throws an exception:

> EInvalidOperation - 'Control '' has no parent window'

> These items are created dynamically based on system information.

> On the Create event for the form, I have the following:

>     MyTree=new TMyTreeView(Form1);
>     MyTree->Align=alClient;
>     MyTree->Parent=Form1;

> Any ideas would be greatly appreciated.

> Thanks,

> Jason Dentler

Re:TTreeNodes->Add before TCustomTreeView->Parent set


In that case, you can populate the control inside the inherited Loaded()
function

Gambit

Quote
"Anonymous" <anonym...@asdf.com> wrote in message news:3bca0855_1@dnews...
> I'm writing a completely self-contained component. I need to be able to
> automatically pre-populate this tree view with items some time after it
has
> a parent, but before it is displayed. Any ideas?

Re:TTreeNodes->Add before TCustomTreeView->Parent set


Hello Anonymous,

"Anonymous" <anonym...@asdf.com> schreef in bericht news:3bc89f80_2@dnews...

Quote
> I need to dynamically populate a TCustomTreeView descendant class before
it
> is displayed on the form.

> I'm trying to pre-populate a TCustomTreeView descendant class in the
> constructor. When I do this, the TCustomTreeView->Items->Add(Null, Str)
line
> throws an exception:

> EInvalidOperation - 'Control '' has no parent window'

This is a common error when you try to populate a component before it is
created and has handles allocated and such.
To populate such a component you should override the CreateWnd() method:

// in header
protected:
    vitual void __fastcall CreateWnd();

// in source
void __fastcall TMyTree::CreateWnd()
{
    TCustomTreeView::CreateWnd();
    PopulateMyTreeView();

Quote
}

Hope this helps...;-))

Quote
> Jason Dentler

--
Greetings from sunny Amsterdam

         Jan

email: bijs...@worldonline.nl
http://home.worldonline.nl/~bijster

Other Threads