Chapter 19. Filling a window

Adding a button

lets add a button to our sample program. In the constructor for the frame, we add:

new wxButton(this, wxID_YES);

This will create a new button. The parent window for the button will be the frame (this). The id will be wxID_OK which is the default ID for any OK button. the wxButton constructor has many more optional arguments:

wxButton(wxWindow* parent, wxWindowID id, 
const wxString& label = wxEmptyString, 
const wxPoint& pos = wxDefaultPosition, 
const wxSize& size = wxDefaultSize, 
long style = 0, 
const wxValidator& validator = wxDefaultValidator, 
const wxString& name = "button")

The individual parameters are:


the parent window for the button. In this case, it is the current frame. In many cases, this can be a panel, a notebook, etc.


the ID for the item. Connect this via your event table to call your functions. If you use default ids you will get default behavior.


the position of the item, given from the top-left corner of the parent. Use wxPoint(int x, int y) to create a point object.


the size of the item. Use wxSize(int width, int height) to create a size object.


extra style attributes. can be any combination of: wxBU_LEFT, wxBU_TOP, wxBU_RIGHT, wxBU_BOTTOM, wxBU_EXACTFIT, wxNO_BORDER joined with a logical or ( | ). Example: wxBU_LEFT | wxNO_BORDER


a validator can be used to check input data and have input data automatically retrieved and stored in a variable. This is useless for buttons, but useful for text fields.


a name for the control. Absolutely useless on modern systems. Leave the default value in there.

Please note: Even though we did not set the label, and it defaults to an empty string, the button will still display "Yes". This is because we use the default id for a "yes" button. On an international machine this will automatically translate into the language that is set, e.g. "Ja" on a German machine, "Si" on a Spanish one, etc.

Practice: Add a new button labeled "Bla" with the event id "ID_BLA". It should be at position 50,30 in the window, and have no border.

To connect a button, use the EVT_BUTTON in your event table:

EVT_BUTTON(wxID_YES, MyFrame::buttonPressed)