Table of Contents

    1. 1.

      This example shows how we can have two widgets in a wxSizer and switch between which one is displayed. This is not limited to two widgets and is generally a way we can keep widgets around but not displayed and display them at a later time. The following screenshots illustrate this.

      While it may appear that all we have done is change the label on the button, in fact these are two separate widgets with labels A and B and we are swapping them in and out as we click the Toggle button.

      We create a top-level frame and wxBoxSizer to manage its children.
      f = RFrame("Sizer Show Example")
      sz = wxBoxSizer(wxHORIZONTAL)
      

      We create three buttons. The first is the one we will use to toggle the display of the other two. That is, we will have a callback on the button labelled "Toggle" and it will change the currently displayed other button from A to B to A and so on.

      btn = wxButton(f, wxID_ANY, "Toggle")
      sz$Add(btn)
      
      a = wxButton(f, wxID_ANY, "A")
      sz$Add(a, 1, wxEXPAND)
      b = wxButton(f, wxID_ANY, "B")
      sz$Add(b, 1, wxEXPAND)
      b$Show(FALSE)
      

      Note that we hid button b so it would not be displayed in the initial view.

      Now we specify the callback for the Toggle button. If the global variable ctr is TRUE, then we hide button a and show button b. Otherwise, we do the opposite. Note that we are calling the Show method on the sizer (not the wxWindow) to display the button of interest. We hide the other button by calling its Show method. These are different Show methods: one is for a wxWindow and the other is for a wxSizer. At the end of the hiding and showing steps, we tell the wxSizer to arrange its children again via a call Layout.

      
      ctr = TRUE
      btn$AddCallback(wxEVT_COMMAND_BUTTON_CLICKED, 
                       function(ev) {
                         if(ctr) {
                           a$Show(FALSE)
                           sz$Show(b)
                         } else {
                           b$Show(FALSE)
                           sz$Show(a)
                         }
                         sz$Layout()
                         ctr <<- !ctr
                         TRUE
                       })
      
      

      f$SetSizer(sz)
      sz$SetSizeHints(f)
      f$Show()
      
      wxEventLoop()$Run()