Page 1 of 1

How to clean, populate and refresh a wxPanel?

Posted: Sat Aug 01, 2009 10:49 pm
by Sunsawe
Hi,

So as the title says, I would like to clean a wxPanel; meaning, erasing every child it may have. Then I would like to assign it a new sizer with and new "children". Finally I would like to update the view.
This is the code I use:

Code: Select all

//page is the panel
page->DestroyChildren();

fgs = new wxFlexGridSizer(0,2,1,1);
fgs->AddGrowableCol(1);

fgs->Add( new wxStaticText(page,wxID_ANY,wxT("Name :")));
fgs->Add( new wxStaticText(page,wxID_ANY,wxT("Test Failure")),1,wxEXPAND,0);

if(condition)
{
fgs->Add( new wxStaticText(page,wxID_ANY,wxT("Level :")));
fgs->Add( new wxStaticText(page,wxID_ANY,wxT("Critical Failure")),1,wxEXPAND,0);
}

page->SetSizer(fgs);
page->Refresh();
With this, every new element is added to the very first line of the panel (so the texts are on top of each others). But when I resize the window, everything gets in order. The lines and columns are respected and the previous elements are gone.
I tried to replace the Refresh method by Update, but the result was the same, meaning, like when they were actually not called at all.

So does DestroyChildren eliminates previous sizers as well?
Could someone explain what I am doing wrong?

Thank you

Posted: Sun Aug 02, 2009 7:04 am
by catalin
Try hiding all children before the call to destroy them.

What I think happens is this: the old windows will be destroyed at idle time, and the first idle event will be received after all other code there (including Refresh) is executed. So you only see the difference at the next Refresh event.

Posted: Sun Aug 02, 2009 8:19 am
by Sunsawe
I am sorry but how do you hide the children of a wxWindow?

I try to hide the complete panel before modifying it and re-show it afterwards. It did not change anything.

Posted: Sun Aug 02, 2009 9:43 am
by DavidHart
Hi,

You could try a Layout() call at the end of that code.

But why do this? It would be easier to delete the panel itself and create a new one. And easier still if you subclass wxPanel, then put the above code in its ctor.

Regards,

David

Posted: Sun Aug 02, 2009 8:18 pm
by Sunsawe
Actually, I don't want to destroy and recreate the panel because it can come from different places and already a part of a complex module.
It seems simpler to just clean it than to find out where it is coming from and in which sizers it is already included then replace the new panel in that place.

But thank you for your answer, it did the trick.