OVERVIEW
A wxCheckBox acts as a toggle control – either it is checked or not checked. We can set a 3-state checkbox as well, but that is non-standard and not supported in all platforms. By default the label will appear to the right of the checkbox, but this can be changed also.
SAMPLE CODE
checkbox.h
#include <wx/wxprec.h>
#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif
class Checkbox: public wxFrame {
public:
Checkbox(const wxString &title);
void toggle1(wxCommandEvent &event);
void toggle2(wxCommandEvent &event);
private:
wxCheckBox *mCk1, *mCk2;
};
checkbox.cpp
#include "checkbox.h"
Checkbox::Checkbox(const wxString &title):
wxFrame(NULL, -1, title, wxDefaultPosition, wxSize(500,200)) {
wxPanel *panel = new wxPanel(this, -1);
wxBoxSizer *vbox = new wxBoxSizer(wxVERTICAL);
panel->SetSizer(vbox);
mCk1 = new wxCheckBox(panel, -1, wxT("This is a simple checkbox"));
mCk1->Bind(wxEVT_COMMAND_CHECKBOX_CLICKED, &Checkbox::toggle1, this);
mCk2 = new wxCheckBox(panel, -1, wxT("Label to the left"), wxDefaultPosition, wxDefaultSize, wxALIGN_RIGHT);
mCk2->Bind(wxEVT_COMMAND_CHECKBOX_CLICKED, &Checkbox::toggle2, this);
vbox->Add(mCk1, 1, wxEXPAND);
vbox->Add(mCk2, 1, wxEXPAND);
Center();
}
void Checkbox::toggle1(wxCommandEvent &event) {
if (mCk1->GetValue())
wxMessageBox(wxT("checked"));
else
wxMessageBox(wxT("not checked"));
}
void Checkbox::toggle2(wxCommandEvent &event) {
if (mCk2->GetValue())
wxMessageBox(wxT("checked"));
else
wxMessageBox(wxT("not checked"));
}
main.h
#include <wx/wxprec.h>
#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif
class Main: public wxApp {
public:
virtual bool OnInit();
};
main.cpp
#include "main.h"
#include "checkbox.h"
IMPLEMENT_APP(Main)
bool Main::OnInit() {
Checkbox *app = new Checkbox(wxT("Checkbox"));
app->Show(true);
}
The output is shown below
Leave a Reply