The wxButton (wxWidgets: wxButton Class Reference) control implements a button.


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.
 File: StandardControls/WxButton1/src/WxButton1Frame.h

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

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

    void OnButtonClicked(wxCommandEvent& evt);

    wxTextCtrl* m_textCtrl;
    unsigned int m_buttonClicksCount;


 File: StandardControls/WxButton1/src/WxButton1Frame.cpp
#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);

    // 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);

void WxButton1Frame::OnButtonClicked(wxCommandEvent& evt)

    if (m_textCtrl)
        std::wstringstream contents;
        contents << L"Button has been clicked "
            << m_buttonClicksCount << " times";


// 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)

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.

Figure 1: The WxButton1 Application

Figure 2: The WxButton1 Application (after the button has been pressed a few times)

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

 File: StandardControls/WxButton1/src/WindowIDs.h

#include <wx/defs.h>

const wxWindowID ButtonID = wxID_HIGHEST + 1;

 File: StandardControls/WxButton1/src/WxButton1App.h

 #include <wx/app.h>

 class WxButton1App : public wxApp
    virtual bool OnInit();

 File: StandardControls/WxButton1/src/WxButton1App.cpp
#include "WxButton1App.h"
#include "WxButton1Frame.h"


bool WxButton1App::OnInit()
    WxButton1Frame* frame = new WxButton1Frame("WxButton1");
    return true;