Search code examples
c++wxwidgets

wxFrame does not process Tab button


I have a simple wxFrame with 3 buttons. After I press Tab nothing happens. In the forum I found that wxFrame should process Tab button events normally and switch focus between controls. I tried with wxTAB_TRAVERSAL and without it, but look like no result.

Here is my code. wxWidgets 3.0.2. Please, help.

class TabWnd
    : public wxFrame
{
public:
    TabWnd()
        : wxFrame(nullptr,
                  wxID_ANY,
                  wxEmptyString,
                  wxDefaultPosition,
                  wxDefaultSize,
                  wxDEFAULT_FRAME_STYLE | wxTAB_TRAVERSAL)
    {
        wxBoxSizer* sz = new wxBoxSizer(wxVERTICAL);

        wxButton* b1 = new wxButton(this, wxID_ANY, wxT("First"));
        sz->Add(b1, 0, wxALL, 5);

        wxButton* b2 = new wxButton(this, wxID_ANY, wxT("Second"));
        sz->Add(b2, 0, wxALL, 5);

        wxButton* b3 = new wxButton(this, wxID_ANY, wxT("Third"));
        sz->Add(b3, 0, wxALL, 5);

        SetSizer(sz);
        Layout();
        Centre(wxBOTH);
    }
};

class WxguiApp
    : public wxApp
{
public:
    bool OnInit() override
    {
        TabWnd* mainWnd = new TabWnd();
        mainWnd->Show();
        SetTopWindow(mainWnd);

        return true;
    }
};

IMPLEMENT_APP(WxguiApp);

Solution

  • Try adding a panel between the frame and the buttons like this:

    wxBoxSizer* sz = new wxBoxSizer(wxVERTICAL);
    
    wxPanel* pnl = new wxPanel( this, wxID_ANY );
    wxBoxSizer* sz2 = new wxBoxSizer( wxVERTICAL );
    
    wxButton* b1 = new wxButton(pnl, wxID_ANY, wxT("First"));
    sz2->Add(b1, 0, wxALL, 5);
    
    wxButton* b2 = new wxButton(pnl, wxID_ANY, wxT("Second"));
    sz2->Add(b2, 0, wxALL, 5);
    
    wxButton* b3 = new wxButton(pnl, wxID_ANY, wxT("Third"));
    sz2->Add(b3, 0, wxALL, 5);
    
    pnl->SetSizer( sz2 );
    sz->Add( pnl, 1, wxEXPAND );
    
    SetSizer(sz);
    Layout();
    Centre(wxBOTH);