wxRadioBox
Introduction
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.
|
Example
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.
#ifndef _TUTORIALS_WXWIDGETS_WXRADIOBOX1FRAME_H_
#define _TUTORIALS_WXWIDGETS_WXRADIOBOX1FRAME_H_
#include <wx/frame.h>
#include <wx/radiobox.h>
#include <wx/textctrl.h>
class WxRadioBox1Frame : public wxFrame
{
public:
WxRadioBox1Frame(const wxString& title);
private:
void OnRadioBoxChange(wxCommandEvent& evt);
private:
wxRadioBox* m_radioBox;
wxTextCtrl* m_textctrl;
wxDECLARE_EVENT_TABLE();
};
#endif
#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,
m_radioBox->GetString(m_radioBox->GetSelection()),
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);
panelSizer->AddSpacer(15);
panel->SetSizer(panelSizer);
// 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);
SetSizerAndFit(topSizer);
}
void WxRadioBox1Frame::OnRadioBoxChange(wxCommandEvent& evt)
{
wxString text = m_radioBox->GetString(evt.GetSelection());
m_textctrl->SetValue(text);
}
wxBEGIN_EVENT_TABLE(WxRadioBox1Frame, wxFrame)
EVT_RADIOBOX(wxID_RADIOBOX, WxRadioBox1Frame::OnRadioBoxChange)
wxEND_EVENT_TABLE()
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.
|
|
The rest of the source files don't contain significant changes but are shown here for completeness.
#ifndef _TUTORIALS_WXWIDGETS_WINDOWIDS_H_ #define _TUTORIALS_WXWIDGETS_WINDOWIDS_H_ #include <wx/defs.h> const wxWindowID wxID_RADIOBOX = wxID_HIGHEST + 1; #endif
#ifndef _TUTORIALS_WXWIDGETS_WXRADIOBOX1APP_H_
#define _TUTORIALS_WXWIDGETS_WXRADIOBOX1APP_H_
#include <wx/app.h>
class WxRadioBox1App : public wxApp
{
public:
virtual bool OnInit();
};
#endif
