The wxRadioBox (wxWidgets: wxRadioBox Class Reference) control presents a list of mutually exclusive choices. An example of a wxRadioBox control is shown in Figure 1 below.

Figure 1: The wxRadioBox control


Our first example shows a wxRadioBox control with 3 options. The selected option is displayed in a wxTextCtrl control. The example is a simple modification of the MinimalApp2 example we presented in the minimal application tutorial. The full source for this example is available from our GitHub repository: wxWidgetsTutorials/StandardControls/WxRadioBox1.

The source files are shown below. The following modifications were made:

  • A wxTextCtrl and wxRadioBox have been added to the frame.
  • The OnRadioBoxChange method is the event handler for the radiobox. It updates the text of the wxTextCtrl.
  • The event table links the radiobox and its event handler.
 File: StandardControls/WxRadioBox1/src/WxRadioBox1Frame.h

#include <wx/frame.h>
#include <wx/radiobox.h>
#include <wx/textctrl.h>

class WxRadioBox1Frame : public wxFrame
    WxRadioBox1Frame(const wxString& title);

    void OnRadioBoxChange(wxCommandEvent& evt);

    wxRadioBox* m_radioBox;
    wxTextCtrl* m_textctrl;


 File: StandardControls/WxRadioBox1/src/WxRadioBox1Frame.cpp
#include "WxRadioBox1Frame.h"
#include "WindowIDs.h"
#include <wx/panel.h>
#include <wx/sizer.h>

WxRadioBox1Frame::WxRadioBox1Frame(const wxString& title)
    : wxFrame(NULL, wxID_ANY, title), m_textctrl(0)
    // Create a top-level panel to hold all the contents of the frame
    wxPanel* panel = new wxPanel(this, wxID_ANY);

    // Create the wxRadioBox control
    wxArrayString choices;
    choices.Add("Option 1");
    choices.Add("Option 2");
    choices.Add("Option 3");
    m_radioBox = new wxRadioBox(panel, wxID_RADIOBOX, 
        "Select one of the options", wxDefaultPosition, 
        wxDefaultSize, choices, 3, wxRA_VERTICAL);

    // Create a wxTextCtrl that will show the currently
    // selected option
    m_textctrl = new wxTextCtrl(panel, wxID_ANY, 
    wxDefaultPosition, wxSize(140, wxDefaultCoord));

    // Set up the sizer for the panel
    wxBoxSizer* panelSizer = new wxBoxSizer(wxVERTICAL);
    panelSizer->Add(m_radioBox, 1, wxEXPAND | wxALL, 15);
    panelSizer->Add(m_textctrl, 0, wxCENTER);

    // Set up the sizer for the frame and resize the frame
    // according to its contents
    wxBoxSizer* topSizer = new wxBoxSizer(wxVERTICAL);
    topSizer->SetMinSize(215, 50);
    topSizer->Add(panel, 1, wxEXPAND);

void WxRadioBox1Frame::OnRadioBoxChange(wxCommandEvent& evt)
    wxString text = m_radioBox->GetString(evt.GetSelection());

wxBEGIN_EVENT_TABLE(WxRadioBox1Frame, wxFrame)
    EVT_RADIOBOX(wxID_RADIOBOX, WxRadioBox1Frame::OnRadioBoxChange)

The application is shown below. Figure 2 shows the application at startup and Figure 3 shows it after the selected option has been changed to option 2. You can see that the value of the wxTextCtrl is updated when the selection option is changed.

Figure 2: The WxRadioBox1 application at startup

Figure 2: The WxRadioBox1 application with choice 2 selected

The rest of the source files don't contain significant changes but are shown here for completeness.

 File: StandardControls/WxRadioBox1/src/WindowIDs.h

#include <wx/defs.h>

const wxWindowID wxID_RADIOBOX = wxID_HIGHEST + 1;

 File: StandardControls/WxRadioBox1/src/WxRadioBox1App.h

#include <wx/app.h>

class WxRadioBox1App : public wxApp
    virtual bool OnInit();

 File: StandardControls/WxRadioBox1/src/WxRadioBox1App.cpp
#include "WxRadioBox1App.h"
#include "WxRadioBox1Frame.h"


bool WxRadioBox1App::OnInit()
    WxRadioBox1Frame* frame = new WxRadioBox1Frame("WxRadioBox1");
    return true;