title: wxWidgets Function calling
body: <p>I am currently writing a mine-sweeper program. There is a function which is connected to a menu. When users click on the menu of &quot;Level&quot;, the program will show a submenu of several different levels (which means a different number of blocks). I have only defined  &quot;SetNovice&quot; function now but found an issue that the mines won't be randomly distributed again as what constructor does(which is binding the buttons with &quot;ButtonOnClicked&quot; function to set the mines).</p>
  <p>In SetNovice, I first call &quot;RemoveChild()&quot; function to remove btn on the GUIPanel, then delete the array &quot;MineField&quot;, and then create btn and MineField again as what constructor does, nut the mines are just not there. The mines should be set when calling &quot;ButtonOnClicked&quot; function. As I wrote in SetNovice when creating btn, I did &quot;Bind&quot; btn to ButtonOnClicked, but no mines are set.</p>
  <p>The program is supposed to randomly reset the mines when SetNovice is invoked, but it turns out not doing so. Why?</p>
  <p>&quot;MineField&quot; is an array of integers in which &quot;-1&quot; is where mines are. But I'm not sure why in &quot;SetNovice&quot; function when I try to remove the original MineField to resize to a new one, the function to which buttons are bound to won't set the mines.</p>
  <p>Below is my GUIWindow.h</p>
  <pre><code>#pragma once
  #include &quot;wx/wx.h&quot;
  #include &quot;wx/mediactrl.h&quot;
  #include &quot;wx/hyperlink.h&quot;
  #include &lt;wx/thread.h&gt;
  #include &quot;cuppMediaCtrl.h&quot;
  
  
  class GUIWindow : public wxFrame
  {
  public:
      GUIWindow();
      ~GUIWindow();
  
  
  public:
      int Width = 20; // default
      int Height = 20; // default
      int score = 0;
      int count = 0;
      wxButton **btn; // an array of pointers
      int* MineField = nullptr; // a pointer, determining if mines exist
      bool ISFirstClick = true;
  
  
  
  
      wxPanel* GUIPanel = new wxPanel(this, wxID_ANY);
      wxGridSizer* grid;
  
      wxGridSizer* gridNovice;
      
  
      wxStatusBar* statusBar = CreateStatusBar(3);
  
      void ButtonOnClicked(wxCommandEvent &amp;evt);
      void OnQuit(wxCommandEvent&amp; event);
      void ChangeBtnColorRed(wxCommandEvent&amp; evt);
      void ChangeBtnColorWhite(wxCommandEvent&amp; evt);
      void ChangeBtnColorGreen(wxCommandEvent&amp; evt);
      void ChangeBtnColorBlue(wxCommandEvent&amp; evt);
      void ChangeBtnColorTokyoHot(wxCommandEvent&amp; evt);
  
      void SetNovice(wxCommandEvent&amp; evt);
      void SetIntermediate(wxCommandEvent&amp; evt);
      void SetProfessional(wxCommandEvent&amp; evt);
      void SetMaster(wxCommandEvent&amp; evt);
      void SetGodLevel(wxCommandEvent&amp; evt);
  
  
  
      void Website1(wxCommandEvent&amp; evt);
      void Website2(wxCommandEvent&amp; evt);
      void ShowCreator(wxCommandEvent&amp; evt);
  
      DECLARE_EVENT_TABLE();
  
  };
  </code></pre>
  <p>Below is GUIWindow.cpp (I've removed some unrelated code and fuctions)</p>
  <pre><code>#include &quot;GUIWindow.h&quot;
  
  wxBEGIN_EVENT_TABLE(GUIWindow, wxFrame)
      EVT_BUTTON(wxID_ANY, ButtonOnClicked)
  wxEND_EVENT_TABLE()
  
  
  GUIWindow::GUIWindow() : wxFrame(nullptr, wxID_ANY, &quot;地雷遊戲&quot;, wxPoint(30, 30), wxSize(700, 700))
  {
  
  
      btn = new wxButton *[Width * Height]; // btn == an array of pointers (should allocate new memory)
      grid = new wxGridSizer(Width, Height, 0, 0);  // set a grid pointer to control the grid
  
  
      MineField = new int[Width * Height]; // This is where mines are distributed, using nField to indicate mines
      wxFont font(18, wxFONTFAMILY_ROMAN, wxFONTSTYLE_ITALIC, wxFONTWEIGHT_BOLD, false);
  
  
  
  
  
      wxString str1 = &quot;Current points: &quot;;
      statusBar-&gt;SetStatusText(str1, 0);
  
  
      wxMenuBar* menuBar = new wxMenuBar();
  
  
      wxMenu* menu1 = new wxMenu();
      menuBar-&gt;Append(menu1, wxT(&quot;About&quot;));
  
  
      wxMenu* menu2 = new wxMenu();
      menuBar-&gt;Append(menu2, wxT(&quot;Game&quot;));
  
  
      SetMenuBar(menuBar);
      menu1-&gt;Append(10001, wxT(&quot;Creator&quot;));
  
  
      wxMenu* imp2 = new wxMenu();
      imp2-&gt;Append(10011, wxT(&quot;Tokyo Hot&quot;));
      imp2-&gt;Append(10012, wxT(&quot;White&quot;));
      imp2-&gt;Append(10013, wxT(&quot;Green&quot;));
      imp2-&gt;Append(10014, wxT(&quot;Red&quot;));
      imp2-&gt;Append(10015, wxT(&quot;Blue&quot;));
      menu2-&gt;AppendSubMenu(imp2, wxT(&quot;Theme&quot;));
  
      
  
      wxMenu* imp3 = new wxMenu();
      imp3-&gt;Append(10020, wxT(&quot;Novice&quot;));
      imp3-&gt;Append(wxID_ANY, wxT(&quot;Intermediate&quot;));
      imp3-&gt;Append(wxID_ANY, wxT(&quot;Professional&quot;));
      imp3-&gt;Append(wxID_ANY, wxT(&quot;Master&quot;));
      imp3-&gt;Append(wxID_ANY, wxT(&quot;God-Level&quot;));
      menu2-&gt;AppendSubMenu(imp3, wxT(&quot;Level&quot;));
  
      wxMenu* imp4 = new wxMenu();
      imp4-&gt;Append(10002, &quot;Introduction&quot;);
      imp4-&gt;Append(10003, &quot;Explanation&quot;);
      menu1-&gt;AppendSubMenu(imp4, &quot;Hints&quot;);
  
  
      wxMenuItem* quit = new wxMenuItem();
      quit = new wxMenuItem(menu1, wxID_EXIT, wxT(&quot;Quit\tCtrl+Q&quot;));
      menu1-&gt;Append(quit);
  
  
  
      Connect(wxID_EXIT, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(GUIWindow::OnQuit));
      Centre();
  
      Connect(10001, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(GUIWindow::ShowCreator));
      Connect(10011, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(GUIWindow::ChangeBtnColorTokyoHot));
      Connect(10012, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(GUIWindow::ChangeBtnColorWhite));
      Connect(10013, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(GUIWindow::ChangeBtnColorGreen));
      Connect(10014, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(GUIWindow::ChangeBtnColorRed));
      Connect(10015, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(GUIWindow::ChangeBtnColorBlue));
      Connect(10020, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(GUIWindow::SetNovice));
      Connect(10002, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(GUIWindow::Website1));
      Connect(10003, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(GUIWindow::Website2));
  
  
      
  
      for (int x = 0; x &lt; Width; x++)
      {
          for (int y = 0; y &lt; Height; y++)
          {
              btn[y * Width + x] = new wxButton(GUIPanel, (y * Width + x)); // every pointer points to a button object with class wxButton
              btn[y * Width + x]-&gt;SetFont(font);
              
              grid-&gt;Add(btn[y * Width + x], 1, wxEXPAND, wxALL);
              btn[y * Width + x]-&gt;Bind(wxEVT_COMMAND_BUTTON_CLICKED, &amp;GUIWindow::ButtonOnClicked, this);
              MineField[y * Width + x] = 0; //set each block to 0 (default value)
          }
      }
  
  
  
      GUIPanel-&gt;SetSizer(grid); // &quot;this&quot; is pointing at the parent wxFrame: GUIWindow
      GUIPanel-&gt;Show();
  }
  
  
  
  
  
  
  GUIWindow::~GUIWindow()
  {
      delete[] btn;
  }
  
  void GUIWindow::OnQuit(wxCommandEvent&amp; WXUNUSED(event))
  {
      Close(true);
  }
  
  
  void GUIWindow::SetNovice(wxCommandEvent&amp; evt)
  {
  
      Width = 10;
      Height = 10;
      score = 0;
      
      GUIPanel-&gt;DestroyChildren(); // delete btn, but NOT removing grid.
      delete[] MineField;
      gridNovice = new wxGridSizer(Width, Height, 0, 0);
  
  
      wxFont font(18, wxFONTFAMILY_ROMAN, wxFONTSTYLE_ITALIC, wxFONTWEIGHT_BOLD, false);
      
  
      btn = new wxButton* [Width * Height];
      MineField = new int[Width * Height];
  
  
  
      for (int x = 0; x &lt; Width; x++)
      {
          for (int y = 0; y &lt; Height; y++)
          {
              btn[y * Width + x] = new wxButton(GUIPanel, (y * Width + x)); // every pointer points to a button object with class wxButton
              btn[y * Width + x]-&gt;SetFont(font);
              
              gridNovice-&gt;Add(btn[y * Width + x], 1, wxEXPAND, wxALL);
              btn[y * Width + x]-&gt;Bind(wxEVT_COMMAND_BUTTON_CLICKED, &amp;GUIWindow::ButtonOnClicked, this);
              MineField[y * Width + x] = 0; //set each block to 0 (default value)
          }
      }
  
      GUIPanel-&gt;SetSizer(gridNovice);
      GUIPanel-&gt;Show();
  }
  
  
  
  void GUIWindow::ButtonOnClicked(wxCommandEvent &amp;evt)
  {
      // get position of button
      int x = (evt.GetId()) % Width;
      int y = (evt.GetId()) / Width;
      int mine_count = 0;
  
  
  
      wxString s1 = &quot;Believe in Yourself.&quot;;
      wxString s2 = &quot;You are the PRIDE of NTUEE !&quot;;
      wxString s3 = &quot;TAIWAN need you!!! so DO NOT give up!!!&quot;;
      wxString s4 = &quot;We are all together, blessing you.&quot;;
      wxString s5 = &quot;Who's the best minesweeper ? YOU!!!&quot;;
      wxString s6 = &quot;Get bored? Try other functions in the menu!&quot;;
      wxString s7 = &quot;想脫魯 ? 踩地雷吧 XDXDXD&quot;;
      wxString phraseArr[] = { s1, s2, s3, s4, s5, s6, s7};
      
  
      if (ISFirstClick == true)
      {
          int mine = Width * Height / 2;
  
          while (mine != 0)
          {
              int rx = rand() % Width;
              int ry = rand() % Height;
  
              if (MineField[ry * Width + rx] == 0 &amp;&amp; rx != x &amp;&amp; ry != y)
              {
                  MineField[ry * Width + rx] = -1; // -1 indicates the mine
                  mine--;
              }
          }
  
          ISFirstClick = false; // avoid mines been redistributed again
      }
  
      btn[y * Width + x]-&gt;Enable(false);
  
      if (MineField[y * Width + x] == -1)
      {
          wxMessageBox(&quot;BOOM!&quot;);
          statusBar-&gt;SetStatusText(wxT(&quot;&quot;), 2); // reset the status bar
          this-&gt;count++;
  
          //reset mines
          this -&gt; score = 0;
          ISFirstClick = true;
          for (int x = 0; x &lt; Width; x++)
          {
              for (int y = 0; y &lt; Height; y++)
              {
                  MineField[y * Width + x] = 0;
                  btn[y * Width + x]-&gt;SetLabel(&quot;&quot;);
                  btn[y * Width + x]-&gt;Enable(true);
  
              }
          }
      }
      
  
      else
      {   
          if (this-&gt;count &lt; 7)
          {
              wxString Newstr = phraseArr[this-&gt;count];
              statusBar-&gt;SetStatusText(Newstr, 2);
              this-&gt;count++;
          }
          else if (this-&gt;count &gt;= 7) this-&gt;count = 0;
          
  
          this -&gt; score += 1;
          wxString scoreStr = std::to_string(this-&gt;score);
          statusBar-&gt;SetStatusText(scoreStr, 1);
  
          //count the mines surrounding
          for (int i = -1; i &lt; 2; i++)
          {
              for (int j = -1; j &lt; 2; j++)
              {
                  if (x + i &gt;= 0 &amp;&amp; x + i &lt; Width &amp;&amp; y + i &gt;= 0 &amp;&amp; y + j &lt; Height)
                  {
                      if (MineField[(y + j) * Width + (x + i)] == -1)
                      {
                          mine_count++;
                      }
                  }
              }
          }
  
          if (mine_count &gt;= 0)
          {
              btn[y * Width + x]-&gt;SetLabel(std::to_string(mine_count));
          }
      }
  
      evt.Skip();
  }
  </code></pre>