Return code -11 when performing SetSizerandFit

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
philm
Experienced Solver
Experienced Solver
Posts: 88
Joined: Fri Dec 18, 2015 4:46 am

Return code -11 when performing SetSizerandFit

Post by philm »

Hello all,

I ahev ran into an issue that I can't seem to figure out. The solution is not quite obvious.

So right now, I have 1 frame and 2 panels. The first panel, has 2 buttons in it. When the user clicks on one of the two buttons, the first panel will be deleted and the second one will be created with a few more controls in it (a listbox, static text, and 2 buttons)

Currently, here is a working version of the code for the first panel:

Code: Select all

    wxBoxSizer *buttonSizer = new wxBoxSizer(wxHORIZONTAL);

	// This seciton will create a new panel and apply 2 buttons on the panel. 
	// The 2 buttons are associated with the panel and when the panel is destoryed, so are the buttons.
	_initialStartPanel->Create(this, wxID_ANY);
	
	wxButton *buttonNewFile = new wxButton(_initialStartPanel, buttonID::ID_buttonNew, "New", wxDefaultPosition, wxSize(75, 23));
	wxButton *buttonOpenFile = new wxButton(_initialStartPanel, buttonID::ID_buttonOpen, "Open", wxDefaultPosition, wxSize(75, 23));
    
    buttonSizer->Add(buttonNewFile, 0, wxALL, 6);
    buttonSizer->Add(buttonOpenFile, 0, wxTOP | wxBOTTOM | wxRIGHT, 6);

    _initialStartPanel->SetSizer(buttonSizer);
    
    SetSizerAndFit(buttonSizer);
    buttonSizer->Fit(_initialStartPanel);
    
    this->SetSize(_initialStartPanel->GetSize());
    this->SetMaxSize(this->GetSize());

    _UIState = systemState::INITIAL_START_UP;
Here is the code for the second panel:

Code: Select all

    wxPanel *problemSelectPanel = new wxPanel();
    wxBoxSizer *footerSizer = new wxBoxSizer(wxHORIZONTAL);
    wxBoxSizer *topSizer = new wxBoxSizer(wxVERTICAL);

	problemSelectPanel->Create(this, wxID_ANY);
	
    wxStaticText *text = new wxStaticText(problemSelectPanel, wxID_ANY, "Select Physics Problem:");
    text->SetFont(*font);
    
    _physicsProblemsListBox->Create(problemSelectPanel, generalFrameButton::ID_LISTBOX, wxDefaultPosition, wxDefaultSize, arrayPhysicsProblem, wxLB_SINGLE); 
    
    topSizer->Add(text, 0, wxALL | wxALIGN_LEFT, 6);
    topSizer->Add(_physicsProblemsListBox, 0, wxLEFT | wxRIGHT | wxBOTTOM | wxALIGN_LEFT, 6);
    
	wxButton *backButton = new wxButton(problemSelectPanel, buttonID::ID_buttonBack, "Back", wxDefaultPosition, wxSize(75, 23));
	wxButton *finishButton = new wxButton(problemSelectPanel, buttonID::ID_buttonFinish, "Finish", wxDefaultPosition, wxSize(75, 23));
    
    footerSizer->Add(backButton, 0, wxCENTER | wxLEFT | wxRIGHT | wxBOTTOM, 6);
    footerSizer->Add(finishButton, 0, wxCENTER | wxBOTTOM | wxRIGHT, 6);
    
    topSizer->Add(footerSizer, 0, wxALIGN_RIGHT);
    
    problemSelectPanel->SetSizer(topSizer);
    
    SetSizerAndFit(topSizer, false);// The error is here!
    topSizer->Fit(problemSelectPanel);
    
    this->SetSize(problemSelectPanel->GetSize());
    this->SetMaxSize(problemSelectPanel->GetSize());
    
    _UIState = systemState::PHYSICS_CHOOSING;

Interestingly enough, when bypass the first panel and have the second panel draw first, everything is all good. No error codes.

I have a suspicion that something is not being properly destroyed when the frame transitions between panels. But I thought that when you call the destroy function for the panel, it destroys everything including sizers. Is this correct or is there something that I have to do special with the sizers?
Last edited by philm on Mon Mar 06, 2017 6:32 pm, edited 1 time in total.
catalin
Moderator
Moderator
Posts: 1618
Joined: Wed Nov 12, 2008 7:23 am
Location: Romania

Re: Return code -11 when performing SetSizerandFit

Post by catalin »

First of all, you have way too much code for such a question. For example, any wxFont-related code is completely irrelevant here.
As your first attempt at solving this, you need to remove all irrelevant code and maybe already see what is wrong and where.
philm wrote:

Code: Select all

    _initialStartPanel->SetSizer(buttonSizer);
    
    SetSizerAndFit(buttonSizer);

Code: Select all

    problemSelectPanel->SetSizer(topSizer);
    
    SetSizerAndFit(topSizer, false);// The error is here!
You are not allowed to set the same sizer to more than one window.
philm
Experienced Solver
Experienced Solver
Posts: 88
Joined: Fri Dec 18, 2015 4:46 am

Re: Return code -11 when performing SetSizerandFit

Post by philm »

Hello Catalin,

Thank you for the advice and tip. I am looking to improve my posts whereever I go. Sometimes, I might put in more detail then needed but I want to make sure that who ever reads the post will be on the same page as I.

I have now truncated the code to display mainly the relevent parts.

I am currently not at my computer with the code, but once I am avaiable, I will give it a shot. I am still learning how to work with sizers. I made about 8 dialog boxes with them previousely so I hae some experience. I am currently learning how I can use them in a panel. Previousely, I would create everything on a dialog window. Now, I am using panels on a frame.

So I am thinking, would it be better to call:

Code: Select all

problemSelectPanel->SetSizerAndFit(topSizer);
Post Reply