Search code examples
c++wxwidgets

Add a panel when a button is clicked? (beginner)


I want to add a panel to my main window, when a button is clicked (Like a menu where you can switch between home and e.g. settings). I'm still learning wxWidgets, so I don't know most of the things. I searched the Web but found nothing that really helped me. Here's me Code: `

#include "MainFrame.h"
#include <wx/wx.h>

MainFrame::MainFrame(const wxString& title): wxFrame(nullptr, wxID_ANY, title, wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE & ~wxRESIZE_BORDER & ~wxMAXIMIZE_BOX) {
    //Startfenster erstellen
    wxPanel* homePanel = new wxPanel(this, wxID_ANY, wxPoint(50, 0), wxSize(1550, 900));
    homePanel->SetBackgroundColour(wxColour(*wxWHITE));
    wxPanel* menue = new wxPanel(this, wxID_ANY, wxDefaultPosition, wxSize(50, 900));

    menue->SetBackgroundColour(wxColor(100, 100, 100));

    //Startfenster gestalten
    wxStaticText* welcomeText = new wxStaticText(homePanel, wxID_ANY, "Willkommen!", wxPoint(600, 100), wxDefaultSize);
    welcomeText->SetFont(wxFont(50, wxFONTFAMILY_DEFAULT , wxFONTSTYLE_ITALIC, wxFONTWEIGHT_NORMAL));
    
    wxInitAllImageHandlers();

    //Bitmaps erstellen
    wxBitmap homesymbol;
    homesymbol.LoadFile(wxT("homesymbol.png"), wxBITMAP_TYPE_PNG);
    wxBitmap settingssymbol;
    settingssymbol.LoadFile(wxT("settingssymbol.png"), wxBITMAP_TYPE_PNG);
    wxBitmap timetablesymbol;
    timetablesymbol.LoadFile(wxT("timetablesymbol.png"), wxBITMAP_TYPE_PNG);


    //Knöpfe zum Menü hinzufügen
    wxBitmapButton* homeButton = new wxBitmapButton(menue, wxID_ANY, homesymbol, wxDefaultPosition, wxSize(50, 50), wxBU_AUTODRAW | wxNO_BORDER);
    wxBitmapButton* settingsButton = new wxBitmapButton(menue, wxID_ANY, settingssymbol, wxPoint(0, 850), wxSize(50, 50), wxBU_AUTODRAW|wxNO_BORDER);
    wxBitmapButton* timetableButton = new wxBitmapButton(menue, wxID_ANY, timetablesymbol, wxPoint(0, 50), wxSize(50, 50), wxBU_AUTODRAW | wxNO_BORDER);

    //Knöpfe ein Event zusteilen
    homeButton->Bind(wxEVT_BUTTON, &MainFrame::OnHomeClicked, this);

    CreateStatusBar();
}

void MainFrame::OnHomeClicked(wxCommandEvent& evt) {
    
}

` I got the code from OttoBotCode on YouTube. There's also a App.cpp App.h and MainFrame.h. Tell me if you also need the other ones!

Thanks!

What I tried: Make a sizer in the MainFrame.h class, add in the MainFrame.cpp class and control it in the void OnButtonClicked What I expected: I expected to show a panel that shows up when a button is pressed


Solution

  • SOLUTION:

    In the Header-File create the wxPanel with wxPanel* panel; (also create any controls you want to add/remove later on the panel.

    In the Eventmethod, use:

    panel->RemoveChild(exampleButton);
    panel->Refresh();
    

    to remove Child "exampleButton"m which you created in the Header-File.