wxButton
Introduction
The wxButton (wxWidgets: wxButton Class Reference) control implements a button.
Example
The wxButton control is easy to use. Below we show a simple example of a button with an event handler that updates the value of a wxTextCtrl control when the button is clicked. 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/WxButton1.
The source files are shown below. The following modifications were made:
- A wxTextCtrl and wxButton have been added to the frame.
- The OnButtonClicked method is the event handler for the button. It increments a counter and updates the wxTextCtrl.
- The event table links the button and its event handler.
#ifndef _TUTORIALS_WXWIDGETS_WXBUTTON1FRAME_H_
#define _TUTORIALS_WXWIDGETS_WXBUTTON1FRAME_H_
#include <wx/frame.h>
#include <wx/textctrl.h>
class WxButton1Frame : public wxFrame
{
public:
    WxButton1Frame(const wxString& title);
private:
    void OnButtonClicked(wxCommandEvent& evt);
private:
    wxTextCtrl* m_textCtrl;
    unsigned int m_buttonClicksCount;
    wxDECLARE_EVENT_TABLE();
};
#endif
                    #include "WxButton1Frame.h"
#include "WindowIDs.h"
#include <wx/panel.h>
#include <wx/sizer.h>
#include <wx/button.h>
#include <sstream>
WxButton1Frame::WxButton1Frame(const wxString& title)
    : wxFrame(NULL, wxID_ANY, title), m_textCtrl(0), m_buttonClicksCount(0)
{
    // Create a top-level panel to hold all the contents of the frame
    wxPanel* panel = new wxPanel(this, wxID_ANY);
    // Create the button widget
    wxButton* button = new wxButton(panel, ButtonID, L"Button One");
    // Add a wxTextCtrl that will be updated when the button is clicked
    m_textCtrl = new wxTextCtrl(panel, wxID_ANY,
        L"Button has never been clicked.", wxDefaultPosition, 
        wxSize(200, wxDefaultCoord));
    // Set up the sizer for the panel
    wxBoxSizer* panelSizer = new wxBoxSizer(wxHORIZONTAL);
    panelSizer->Add(button, 0, wxEXPAND);
    panelSizer->Add(m_textCtrl, 1, wxEXPAND);
    panel->SetSizer(panelSizer);
    // Set up the sizer for the frame and resize the frame
    // according to its contents
    wxBoxSizer* topSizer = new wxBoxSizer(wxHORIZONTAL);
    topSizer->Add(panel, 1, wxEXPAND);
    SetSizerAndFit(topSizer);
}
void WxButton1Frame::OnButtonClicked(wxCommandEvent& evt)
{
    ++m_buttonClicksCount;
    if (m_textCtrl)
    {
        std::wstringstream contents;
        contents << L"Button has been clicked "
            << m_buttonClicksCount << " times";
        m_textCtrl->SetValue(contents.str());
    }
    evt.Skip();
}
// Add the event handler to the event table. As you can see we use
// the window ID to link the event handler to the wxButton we created.
wxBEGIN_EVENT_TABLE(WxButton1Frame, wxFrame)
    EVT_BUTTON(ButtonID, WxButton1Frame::OnButtonClicked)
wxEND_EVENT_TABLE()
                    The application is shown below. Figure 1 shows the application when the button has never been pressed and Figure 2 shows it after the button has been pressed 3 times.
|   | 
|   | 
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 ButtonID = wxID_HIGHEST + 1; #endif
 #ifndef _TUTORIALS_WXWIDGETS_WXBUTTON1APP_H_
 #define _TUTORIALS_WXWIDGETS_WXBUTTON1APP_H_
 #include <wx/app.h>
 class WxButton1App : public wxApp
{
public:
    virtual bool OnInit();
};
#endif
                    