title: Panel class module initialization in C++ GUIapp
body: <p>I am a novice in C++ and wxWidgets, and use wxFormBuilder to help me create the GUI.</p>
  <p>I like the idea to put similar functions together in 1 module, which means that my apps have multiple modules.
  One of the modules is for the GUI (it now contains code for the main Frame (with only a menu and taskbar) and
  a Panel (with 2 StaticText controls and a button).
  The button increments a counter and the value is shown in one of the StaticTexts on the Panel.</p>
  <p>Sofar so good. (compiles without errors)
  I can make the Panel show/Hide, but I seem to miss an essential piece of knowledge to make the Panel button work.
  The ways I tried are all similar to the way the Main module is coded, but that is not working.</p>
  <p>I understand how it works with GUI class elements in the same file.
  However, I like to keep all GUI code in one module (GUIFrame.h/.cpp),
  and all the 'function' code in e.g. the Panel module (MyPanel.h/.cpp).</p>
  <p>Just because I am not sure where I make my mistake, I present all code in this post.
  My aplologies if it is too much.</p>
  <p>Hopefully someone can help me bridge my gap in knowledge about this way of working.</p>
  <p>Regards,
  Ruud</p>
  <p>=== GUIFrame.h</p>
  <pre><code>#pragma once
  #include &lt;wx/artprov.h&gt;
  #include &lt;wx/xrc/xmlres.h&gt;
  #include &lt;wx/string.h&gt;
  #include &lt;wx/bitmap.h&gt;
  #include &lt;wx/image.h&gt;
  #include &lt;wx/icon.h&gt;
  #include &lt;wx/menu.h&gt;
  #include &lt;wx/gdicmn.h&gt;
  #include &lt;wx/font.h&gt;
  #include &lt;wx/colour.h&gt;
  #include &lt;wx/settings.h&gt;
  #include &lt;wx/statusbr.h&gt;
  #include &lt;wx/frame.h&gt;
  #include &lt;wx/stattext.h&gt;
  #include &lt;wx/button.h&gt;
  #include &lt;wx/sizer.h&gt;
  #include &lt;wx/panel.h&gt;
  ///////////////////////////////////////////////////////////////////////////
  #define idMenuQuit 1000
  ///////////////////////////////////////////////////////////////////////////////
  /// Class GUIFrame
  ///////////////////////////////////////////////////////////////////////////////
  class GUIFrame : public wxFrame
  {
      private:
  
      protected:
          wxMenuBar* mbar;
          wxMenu* mainMenu;
          wxStatusBar* statusBar;
  
          // Virtual event handlers, overide them in your derived class
          virtual void OnClose( wxCloseEvent&amp; event ) { event.Skip(); }
          virtual void m_menuItemPanelMainOnMenuSelection( wxCommandEvent&amp; event ) { event.Skip(); }
          virtual void OnQuit( wxCommandEvent&amp; event ) { event.Skip(); }
  
      public:
  
          GUIFrame( wxWindow* parent, wxWindowID id = wxID_ANY, const wxString&amp; title = wxT(&quot;Test application&quot;), const wxPoint&amp; pos = wxDefaultPosition, const wxSize&amp; size = wxSize( 500,500 ), long style = wxDEFAULT_FRAME_STYLE );
          ~GUIFrame();
  
  };
  ///////////////////////////////////////////////////////////////////////////////
  /// Class PanelMAIN
  ///////////////////////////////////////////////////////////////////////////////
  class PanelMAIN : public wxPanel
  {
      private:
  
      protected:
          wxStaticText* m_staticText3;
  
          // Virtual event handlers, overide them in your derived class
          virtual void m_btn_Counter_OnButtonClick( wxCommandEvent&amp; event ) { event.Skip(); }
  
      public:
          wxStaticText* m_staticTextPMain;
          wxButton* m_buttonPMain;
  
          PanelMAIN( wxWindow* parent, wxWindowID id = wxID_ANY, const wxPoint&amp; pos = wxPoint( 3,3 ), const wxSize&amp; size = wxSize( 350,300 ), long style = wxBORDER_RAISED, const wxString&amp; name = wxEmptyString );
          ~PanelMAIN();
  
  };
  </code></pre>
  <p>==== GUIFrame.cpp</p>
  <pre><code>#include &quot;wx/wxprec.h&quot;
  #ifdef __BORLANDC__
  #pragma hdrstop
  #endif //__BORLANDC__
  #ifndef WX_PRECOMP
  #include &lt;wx/wx.h&gt;
  #endif //WX_PRECOMP
  #include &quot;GUIFrame.h&quot;
  ///////////////////////////////////////////////////////////////////////////
  GUIFrame::GUIFrame( wxWindow* parent, wxWindowID id, const wxString&amp; title, const wxPoint&amp; pos, const wxSize&amp; size, long style ) : wxFrame( parent, id, title, pos, size, style )
  {
      this-&gt;SetSizeHints( wxDefaultSize, wxDefaultSize );
  
      mbar = new wxMenuBar( 0 );
      mainMenu = new wxMenu();
      wxMenuItem* m_menuItemPanelMain;
      m_menuItemPanelMain = new wxMenuItem( mainMenu, wxID_ANY, wxString( wxT(&quot;To Main Panel&quot;) ) , wxEmptyString, wxITEM_NORMAL );
      mainMenu-&gt;Append( m_menuItemPanelMain );
  
      wxMenuItem* menuFileQuit;
      menuFileQuit = new wxMenuItem( mainMenu, idMenuQuit, wxString( wxT(&quot;&amp;Quit&quot;) ) + wxT('\t') + wxT(&quot;Alt+F4&quot;), wxT(&quot;Quit the application&quot;), wxITEM_NORMAL );
      mainMenu-&gt;Append( menuFileQuit );
  
      mbar-&gt;Append( mainMenu, wxT(&quot;&amp;Menu&quot;) );
  
      this-&gt;SetMenuBar( mbar );
  
      statusBar = this-&gt;CreateStatusBar( 2, wxSTB_SIZEGRIP, wxID_ANY );
  
      // Connect Events
      this-&gt;Connect( wxEVT_CLOSE_WINDOW, wxCloseEventHandler( GUIFrame::OnClose ) );
      mainMenu-&gt;Bind(wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler( GUIFrame::m_menuItemPanelMainOnMenuSelection ), this, m_menuItemPanelMain-&gt;GetId());
      mainMenu-&gt;Bind(wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler( GUIFrame::OnQuit ), this, menuFileQuit-&gt;GetId());
  }
  
  GUIFrame::~GUIFrame()
  {
      // Disconnect Events
      this-&gt;Disconnect( wxEVT_CLOSE_WINDOW, wxCloseEventHandler( GUIFrame::OnClose ) );
  
  }
  
  PanelMAIN::PanelMAIN( wxWindow* parent, wxWindowID id, const wxPoint&amp; pos, const wxSize&amp; size, long style, const wxString&amp; name ) : wxPanel( parent, id, pos, size, style, name )
  {
      this-&gt;SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_APPWORKSPACE ) );
  
      wxBoxSizer* bSizer1;
      bSizer1 = new wxBoxSizer( wxVERTICAL );
  
      m_staticTextPMain = new wxStaticText( this, wxID_ANY, wxT(&quot;We are on PanelMAIN now&quot;), wxDefaultPosition, wxDefaultSize, 0 );
      m_staticTextPMain-&gt;Wrap( -1 );
      bSizer1-&gt;Add( m_staticTextPMain, 0, wxALL, 5 );
  
      m_buttonPMain = new wxButton( this, wxID_ANY, wxT(&quot;Counter++&quot;), wxDefaultPosition, wxDefaultSize, 0 );
      bSizer1-&gt;Add( m_buttonPMain, 0, wxALL, 5 );
  
      m_staticText3 = new wxStaticText( this, wxID_ANY, wxT(&quot;0&quot;), wxDefaultPosition, wxDefaultSize, 0 );
      m_staticText3-&gt;Wrap( -1 );
      bSizer1-&gt;Add( m_staticText3, 0, wxALL, 5 );
  
  
      this-&gt;SetSizer( bSizer1 );
      this-&gt;Layout();
  
      // Connect Events
      m_buttonPMain-&gt;Connect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( PanelMAIN::m_btn_Counter_OnButtonClick ), NULL, this );
  }
  
  PanelMAIN::~PanelMAIN()
  {
      // Disconnect Events
      m_buttonPMain-&gt;Disconnect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( PanelMAIN::m_btn_Counter_OnButtonClick ), NULL, this );
  
  }
  </code></pre>
  <p>==== wxPanelAPP.h</p>
  <pre><code>#ifndef WXPANELAPP_H
  #define WXPANELAPP_H
  #include &lt;wx/app.h&gt;
  class wxPanelApp : public wxApp
  {
      public:
          virtual bool OnInit();
  };
  #endif // WXPANELAPP_H
  </code></pre>
  <p>==== wxPanelAPP.cpp</p>
  <pre><code>#ifdef WX_PRECOMP
  #include &quot;wx_pch.h&quot;
  #endif
  #ifdef __BORLANDC__
  #pragma hdrstop
  #endif //__BORLANDC__
  #include &quot;wxPanelApp.h&quot;
  #include &quot;wxPanelMain.h&quot;
  IMPLEMENT_APP(wxPanelApp);
  bool wxPanelApp::OnInit()
  {
      wxPanelFrame* frame = new wxPanelFrame(0L);
      frame-&gt;SetIcon(wxICON(aaaa)); // To Set App Icon
      frame-&gt;Show();
      return true;
  }
  </code></pre>
  <p>==== wxPanelMain.h</p>
  <pre><code>#ifndef WXPANELMAIN_H
  #define WXPANELMAIN_H
  #include &quot;wxPanelApp.h&quot;
  #include &quot;GUIFrame.h&quot;
  #include &quot;MyPanel.h&quot;
  #include &lt;iostream&gt;
  class wxPanelFrame: public GUIFrame
  {
      public:
          wxPanelFrame(wxFrame *frame);
          ~wxPanelFrame();
  
          wxPanel *m_wxpMain = new PanelMAIN(this, wxID_ANY);
  
      private:
          virtual void OnClose(wxCloseEvent&amp; event);
          virtual void OnQuit(wxCommandEvent&amp; event);
  
          void m_menuItemPanelMainOnMenuSelection( wxCommandEvent&amp; event );
  };
  #endif // WXPANELMAIN_H
  </code></pre>
  <p>==== wxPanelMain.cpp</p>
  <pre><code>#ifdef WX_PRECOMP
  #include &quot;wx_pch.h&quot;
  #endif
  #ifdef __BORLANDC__
  #pragma hdrstop
  #endif //__BORLANDC__
  #include &quot;wxPanelMain.h&quot;
  wxPanelFrame::wxPanelFrame(wxFrame *frame) : GUIFrame(frame)
  {
  #if wxUSE_STATUSBAR
      statusBar-&gt;SetStatusText(_(&quot;Simple Panel Test&quot;), 0);
  #endif
  
      // Hide the panel
      m_wxpMain-&gt;Show(false);
  }
  
  wxPanelFrame::~wxPanelFrame()
  {
  
  }
  
  void wxPanelFrame::OnClose(wxCloseEvent &amp;event)
  {
      Destroy();
  }
  
  void wxPanelFrame::OnQuit(wxCommandEvent &amp;event)
  {
      Destroy();
  }
  
  void wxPanelFrame::m_menuItemPanelMainOnMenuSelection( wxCommandEvent&amp; event )
  {
      // Show the panel
      m_wxpMain-&gt;Show(true);
  }
  </code></pre>
  <p>==== MyPanel.h</p>
  <pre><code>#ifndef MYPANEL_H
  #define MYPANEL_H
  #include &quot;GUIFrame.h&quot;
  #include &lt;iostream&gt;
  class MyPanel : public PanelMAIN
  {
      public:
          MyPanel(wxPanel *panel);
          virtual ~MyPanel();
  
          int i{0};
  
      protected:
  
      private:
          void m_btn_Counter_OnButtonClick( wxCommandEvent&amp; event );
  
  };
  #endif // MYPANEL_H
  </code></pre>
  <p>==== MyPanel.cpp</p>
  <pre><code>#include &quot;MyPanel.h&quot;
  MyPanel::MyPanel(wxPanel *panel) : PanelMAIN(panel)
  {
      std::cout &lt;&lt; &quot;MyPanel::MyPanel /*CONSTRUCTOR*/\n&quot;;
  }
  
  MyPanel::~MyPanel()
  {
      std::cout &lt;&lt; &quot;MyPanel::~MyPanel /*DESTRUCTOR*/\n&quot;;
  }
  
  void MyPanel::m_btn_Counter_OnButtonClick( wxCommandEvent&amp; event )
  {
      wxString wxs_Count{};
      i++;
      wxs_Count &lt;&lt; i;
      m_staticText3-&gt;SetLabelText(wxs_Count);
  }
  </code></pre>