wxCheckBox
Introduction
The wxCheckBox (wxWidgets: wxCheckBox Class Reference) control implements a checkbox.
Example
The wxCheckBox control is easy to use. Below we show a simple example of a checkbox with an event handler that updates the value of a wxTextCtrl control when the checkbox is checked and unchecked. 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/WxCheckBox1.
The source files are shown below. The following modifications were made:
- A wxTextCtrl and wxCheckBox have been added to the frame.
- The OnCheckBoxEvent method is the event handler for the checkbox. It updates the text of the wxTextCtrl.
- The event table links the checkbox and its event handler.
#ifndef _TUTORIALS_WXWIDGETS_WXCHECKBOX1FRAME_H_ #define _TUTORIALS_WXWIDGETS_WXCHECKBOX1FRAME_H_ #include <wx/frame.h> #include <wx/textctrl.h> class WxCheckBox1Frame : public wxFrame { public: WxCheckBox1Frame(const wxString& title); private: void OnCheckBoxEvent(wxCommandEvent& evt); private: wxTextCtrl* m_textctrl; wxDECLARE_EVENT_TABLE(); }; #endif
#include "WxCheckBox1Frame.h" #include "WindowIDs.h" #include <wx/panel.h> #include <wx/sizer.h> #include <wx/checkbox.h> WxCheckBox1Frame::WxCheckBox1Frame(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); panel->SetBackgroundColour(*wxWHITE); // Create the wxCheckBox control wxCheckBox* checkbox = new wxCheckBox(panel, wxID_CHECKBOX, "Checkbox 1"); m_textctrl = new wxTextCtrl(panel, wxID_ANY, "Checkbox not checked", wxDefaultPosition, wxSize(140, wxDefaultCoord)); // Set up the sizer for the panel wxBoxSizer* panelSizer = new wxBoxSizer(wxVERTICAL); panelSizer->AddSpacer(15); panelSizer->Add(checkbox, 0, wxCENTER); panelSizer->AddSpacer(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 WxCheckBox1Frame::OnCheckBoxEvent(wxCommandEvent& evt) { if (evt.IsChecked()) { m_textctrl->SetValue("Checkbox checked"); } else { m_textctrl->SetValue("Checkbox not checked"); } } wxBEGIN_EVENT_TABLE(WxCheckBox1Frame, wxFrame) EVT_CHECKBOX(wxID_CHECKBOX, WxCheckBox1Frame::OnCheckBoxEvent) wxEND_EVENT_TABLE()
The application is shown below. Figure 1 shows the application when the checkbox is not checked and Figure 2 shows it when the checkbox is checked.
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_CHECKBOX = wxID_HIGHEST + 1; #endif
#ifndef _TUTORIALS_WXWIDGETS_WXCHECKBOX1APP_H_ #define _TUTORIALS_WXWIDGETS_WXCHECKBOX1APP_H_ #include <wx/app.h> class WxCheckBox1App : public wxApp { public: virtual bool OnInit(); }; #endif
#include "WxCheckBox1App.h" #include "WxCheckBox1Frame.h" wxIMPLEMENT_APP(WxCheckBox1App); bool WxCheckBox1App::OnInit() { WxCheckBox1Frame* frame = new WxCheckBox1Frame("WxCheckBox1"); frame->Show(true); return true; }