Mohammed Li
Mohammed Li

Reputation: 901

wxwidgets connect multiple functions

I am trying to connect a button to two different functions in different classes. The problem is, whenever I connect the second function, the connection to the first one seems to be gone

windowpointer->Connect( wxbuttonID, wxEVT_COMMAND_BUTTON_CLICKED,
(wxObjectEventFunction) (wxEventFunction) (wxCommandEventFunction) &class1::func1, NULL, (wxEvtHandler*) myclass1);

windowpointer->Connect( wxbuttonID, wxEVT_COMMAND_BUTTON_CLICKED,
(wxObjectEventFunction) (wxEventFunction) (wxCommandEventFunction) &class2::func2, NULL, (wxEvtHandler*) myclass2);

I'd appreciate any suggestion to fix this very much

Upvotes: 2

Views: 819

Answers (2)

VZ.
VZ.

Reputation: 22688

The first event handler found gets the event first and if it doesn't call event.Skip() as part of its processing, no other handlers are called. So if you want to use more than one handler for an event you need to ensure that your event handlers do call wxEvent::Skip(). See the event handling processing overview for more information.

Also notice that it's usually a bad idea, i.e. confusing to both the programmer and the user, to handle command events such as wxEVT_BUTTON, in multiple places. You'd expect a button click to be handled exactly once and while you can have multiple handlers for it, it is unlikely to be a good idea.

Upvotes: 4

Martin Ellison
Martin Ellison

Reputation: 1343

The second Connect call will replace the first one.

Maybe have a method (say onXXXButtonClicked) that calls class1::func1 and class2::func2, and Connect the button to onXXXButtonClicked.

Upvotes: -1

Related Questions