Search code examples
c++eventsbuttonevent-handlingwxwidgets

Event handler function to wxButton with Bind() - wxWidgets 3.0


I am trying to create a simple, basic application to start off with wxWidgets. I can already set up a frame and put menu, menu items and buttons on it and it all displays properly.

However I have troubles using Bind() to attach an event handler to the button. To strip the code a bit, I have this :

class MainFrameFunctions
{
public:
void buttonOneClicked(wxCommandEvent & event);
};

void MainFrameFunctions::buttonOneClicked(wxCommandEvent & event)
{
// do something
}

MainFrame::MainFrame(wxWindow *parent, wxWindowID id, const wxString &title, const wxPoint& , const wxSize& , long style, const wxString& ) : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, size1)
{
wxButton * buttonOne = new wxButton( panelOne, buttonOneID, wxT("Show Box"), wxPoint(70,50), wxSize(200,40) );
}

So I wonder, how do I make an event handler with Bind(), to connect the handler function "buttonOneClicked" to buttonOne ? And also where in code do I put the Bind() line ?

EDIT

After recommendation by VZ. I have made some edits to the program, so that the main components now look like this :

class MainFrame: public wxFrame
{
public:
MainFrame(wxWindow *parent, wxWindowID id, const wxString &title, const wxPoint  

    &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, long style=wxDEFAULT_FRAME_STYLE, const wxString &name=wxFrameNameStr);

void OnExit(wxCommandEvent& event);
bool ButtonOneClicked(wxCommandEvent & event);
wxDECLARE_EVENT_TABLE();
};


class CreateNewFrame: public wxFrame
{
public:
CreateNewFrame(wxWindow *parent, wxWindowID id, const wxString &title, const wxPoint& , const wxSize&, long style);
void OnExit(wxCommandEvent& event);
wxDECLARE_EVENT_TABLE();
};


MainFrame::MainFrame(wxWindow *parent, wxWindowID id, const wxString &title, const wxPoint& , const wxSize& , long style, const wxString& )
: wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, size1)
{
 wxButton * buttonOne = new wxButton( panelOne, buttonOneID, wxT("Button One"), wxPoint(70,50), wxSize(200,40), 0, wxDefaultValidator, "PP" );

 // while we are at it, what is the " wxDefaultValidator, "PP" " part about ? I just copied it from an example

 buttonOne->Bind(wxEVT_BUTTON, &MainFrame::ButtonOneClicked, this);
}


bool MainFrame::ButtonOneClicked(wxCommandEvent & event)
{
CreateNewFrame* NewFrame = new CreateNewFrame(this, NewFrameID, "Button One Clicked", position2, size2);
NewFrame->Show( true );

return true;
}

The problem now is, that I get this error message from CodeBlocks 13.12 :

no matching function for call to 'wxButton::Bind(const wxEventTypeTag<wxCommandEvent>&, bool (MainFrame::*)(wxCommandEvent&), MainFrame* const)'|

Solution

  • You can Bind() the event handler as soon as you want your app to start processing the intended events. Many times this is done after building the UI, so in your case at the end of MainFrame() constructor should be fine.

    You can find examples of using Bind() in the samples, just like many other wxWidgets features; see events sample. For your case you can do

    buttonOne->Bind(wxEVT_BUTTON, &MainFrameFunctions::buttonOneClicked, aMainFrameFunctionsPointer);