Problem with panel and sizing it. Topic is solved

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
PaulUK
Knows some wx things
Knows some wx things
Posts: 43
Joined: Wed Nov 18, 2020 12:55 pm

Problem with panel and sizing it.

Post by PaulUK »

Hello all,

I'm working on my first program with wxWidgets and trying to lay out controls onto a panel with two tool bars for this skeleton program, and the frame should be resizable. This is the cpp file shown below and I'm sure that the problem is a newbie issue but when I try to change the window size the panel size remains fixed. I changed the colour of the panel so it can easily be seen. I think there may be an issue with the order of the sizers or how I'm using them. Can anyone see what might be wrong? Thank you.

Code: Select all

#include "MainWindow.h"
#include "wx/toolbar.h"
#include <wx/artprov.h>
//#include "testicon.xpm"
#include "dish32.xpm"

// Static Event Table
wxBEGIN_EVENT_TABLE(MainWindow, wxFrame)
    EVT_TEXT_ENTER(wxID_ANY, MainWindow::OnRadiusEntered)

wxEND_EVENT_TABLE()

MainWindow::MainWindow(wxWindow *parent,
		wxWindowID id,
		const wxString& title,
		const wxPoint& pos,
		const wxSize& size,
		long style,
		const wxString& name):
	wxFrame(parent, id, title, pos, wxSize(500, 400), style, name)
{
    // Declare panel for controls
    wxPanel *panel = new wxPanel(this, -1);
    panel->SetBackgroundColour(wxColor(100, 200, 200));

    // Set frame icon
    wxIcon frameicon(dish32_xpm);
    SetIcon(frameicon);

    // Set menu bar
    wxMenu *fileMenu = new wxMenu;
    wxMenu *helpMenu = new wxMenu;
    helpMenu->Append(wxID_ABOUT, _T("&About...\tF1"), _T("Show about dialog"));
    fileMenu->Append(wxID_EXIT, _T("E&xit\tAlt-X"), _T("Quit this program"));
    wxMenuBar *menuBar = new wxMenuBar();
    menuBar->Append(fileMenu, _T("&File"));
    menuBar->Append(helpMenu, _T("&Help"));
    SetMenuBar(menuBar);

    // Set toolbar1
    wxBoxSizer *vbox = new wxBoxSizer(wxVERTICAL);
    toolbar1 = new wxToolBar(panel, wxID_ANY);

    //  Insert toolbar1 = AddTool...
    toolbar1->AddTool(wxID_EXIT, _("Exit application"), wxArtProvider::GetBitmap("wxART_QUIT"));
    toolbar1->Realize();

    toolbar2 = new wxToolBar(panel, wxID_ANY);
    //  Insert toolbar2 = AddTool...
    toolbar2->AddTool(wxID_EXIT, _("Exit application"), wxArtProvider::GetBitmap("wxART_QUIT"));
    toolbar2->Realize();

    vbox->Add(toolbar1, 0, wxEXPAND);
    vbox->Add(toolbar2, 0, wxEXPAND);

    //vbox->Add(panel, 1, wxEXPAND);


    // Test code here
    wxBoxSizer *hbox1 = new wxBoxSizer(wxHORIZONTAL);
    wxStaticText *EnterRadius = new wxStaticText(panel, wxID_ANY, wxT("Enter radius in mm"));
    wxTextCtrl *RadiusEntered = new wxTextCtrl(panel, wxID_ANY, wxT(""), wxPoint(-1, -1), wxSize(-1, -1), wxTE_PROCESS_ENTER);

    //hbox2->Add(vbox, 1, wxEXPAND);
    hbox1->Add(EnterRadius, 1, wxLEFT | wxTOP , 20);
    hbox1->Add(RadiusEntered, 1, wxRIGHT | wxTOP , 20);
    vbox->Add(hbox2, 1, wxEXPAND);


    SetSizer(vbox);

    // Set status bar
    CreateStatusBar(2);
    SetStatusText(_T("Status Text 1"), 0);
    SetStatusText(_T("Status Text 2"), 1);

}

void MainWindow::OnRadiusEntered(wxCommandEvent& event)
{
    wxMessageBox("MainWindow::OnRadiusEntered");
}

MainWindow::~MainWindow()
{
}
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Problem with panel and sizing it.

Post by doublemax »

I didn't run the code to test, but my first guess would be:
Change:

Code: Select all

SetSizer(vbox);
to:

Code: Select all

panel->SetSizer(vbox);
Use the source, Luke!
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4204
Joined: Sun Jan 03, 2010 5:45 pm

Re: Problem with panel and sizing it.

Post by PB »

The code is unnecessarily long so I may have missed something. Please, next time post only the minimal code to demonstrate the issue, it is actually quite common to find the issue when reducing the code.

What I would do is:
If you add panel's children to vbox sizer, I would set vbox sizer to that panel, not the frame.
PaulUK
Knows some wx things
Knows some wx things
Posts: 43
Joined: Wed Nov 18, 2020 12:55 pm

Re: Problem with panel and sizing it.

Post by PaulUK »

Thank you, I will try that next. I'm still finding my way.

Next time I'll post just the code that I think is the problem area, I posted the whole file thinking that it may be easier to spot the problem, but point noted, thank you.
PaulUK
Knows some wx things
Knows some wx things
Posts: 43
Joined: Wed Nov 18, 2020 12:55 pm

Re: Problem with panel and sizing it.

Post by PaulUK »

I tried the recommendation:

Code: Select all

panel->SetSizer(vbox);
This worked, now the panel is sizing according to the main frame. Thank you for your help.
Post Reply