shimna
shimna

Reputation: 1

wxsizer does not work in a child panel,when added after an event (wxwidgets 2.8 , windows xp)

I have a top level frame (MyFrame Class) which has a child tree (MyTreeCtrl class)and a child panel (MyPanel class) . An object of MyPanel exists in MyTreeCtrl . An event on a node in the tree should populate the panel.I am setting a sizer with the panel ,but it does not get set. When the sizer is set to the panel while setting the sizer for its parent frame, everything is fine. But ,when trying to set panel sizer in a separate function, it does not work.

void MyTreeCtrl::PopulateElementNode(wxTreeItemId &id)
{

//adding panel control elements and setting its sizer


wxBoxSizer *paneltopsizer = new wxBoxSizer(wxVERTICAL);
(this->mypanel)->SetSizer(paneltopsizer);

this->MyListCtrl=new wxListCtrl(this->mypanel,
    wxID_LISTCTRL,wxDefaultPosition,wxSize(245,100),wxLC_REPORT);
wxButton *Add=new wxButton(this->mypanel,wxID_ADD,wxT("Add"));
wxButton *Delete=new wxButton(this->mypanel,wxID_DELETE,wxT("Delete"));
wxButton *ApplyChanges=new wxButton(this->mypanel,wxID_APPLYCHANGES,wxT("ApplyChanges"));

wxBoxSizer *panelbuttonsizer = new wxBoxSizer(wxHORIZONTAL);
panelbuttonsizer->Add(Add,0,wxALL|wxALIGN_RIGHT,5);
panelbuttonsizer->Add(Delete,0,wxALL|wxALIGN_RIGHT,5);
panelbuttonsizer->Add(ApplyChanges,0,wxALL|wxALIGN_RIGHT,5);
paneltopsizer->Add(this->MyListCtrl,1,wxALL|wxEXPAND,10);
paneltopsizer->Add(panelbuttonsizer,0, wxALIGN_CENTER_HORIZONTAL|wxALL, 5);

//panel control ends
}

Upvotes: 0

Views: 955

Answers (2)

Bryan Petty
Bryan Petty

Reputation: 480

I think that part of the problem here is that you set the panel sizer before you even filled the sizer with controls.

Additionally, you should really be either calling SetSizerAndFit() instead of SetSizer() (note: this will definitely need to be done after the controls have been added to the sizer), or alternatively call mypanel->Layout() (note: the panel's layout method, not the sizer) after adding the sizer and controls. The latter approach should work fine with setting the sizer before adding the controls, but still has to happen last. In either case, it's really not even logical to set the panel's sizer before you've adding anything to the sizer.

Upvotes: 1

ravenspoint
ravenspoint

Reputation: 20616

(this->mypanel)->SetSizer(paneltopsizer);

When you call this, you set the instance of MyTreeCtrl to have the new sizer. This deletes any previous sizer that may have been assigned to the MytreeCtrl instance ( Are you sure that is what you want? )

It does nothing else, unless the MytreeCtrl instance later on receives a resize message. But you probably want the sizer to do something, right? So you have to add some code to make the sizer execute.

One way might be to add

paneltopsizer->Layout() at the end of void MyTreeCtrl::PopulateElementNode()

Something will now happen. I do not know if it will be what you want! I do not really understand what you are trying to do.

Upvotes: 0

Related Questions