here is the connect line:
It would be less ugly, and make the comparison easier, to follow the format of your second example:
Connect(ID_Add, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler
and it would be safer to add the last two parameters explicitly, rather than relying on the defaults (fine on this occasion, but a bad habit to get into):
Connect(ID_Add, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(myFrame::OnAddClick), NULL, this
To expand it even further, what you're really doing is:
Connect(ID_Add, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(myFrame::OnAddClick), NULL, this);
where 'this' is an instance of myFrame.
And one more expansion: the second example is also relying on a default value (the macro expands it automatically). What it's actually doing is:
, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(myFrame::buttonClick), NULL, this );
i.e. If the event-type is correct, connect any event; don't check the ID.
Now let's compare:
this->Connect(ID_Add,... NULL, this );
AddButton->Connect( wxID_ANY,... NULL, this );
Doing it the first way, you connect the frame to itself; the second way, the button is connected to the frame.
I am at a lose as to what the ID_ADD is for?
Again, let's compare. In the second way, any ID will be connected. Since a button always uses its own ID, that's not a problem.
However if you used wxID_ANY in the first way, any button click would be connected to OnAddClick(). That's fine if your frame has only one button, but not otherwise!
I suspect it is somehow in play for how events BUBBLE up.
That isn't, no. But bubbling up explains why method one works at all:
- 1) The button is clicked, so it generates an event and tries to process it itself.
2) Its default handler doesn't care about the event, so it calls the next handler in the chain. That'll be the parent, a panel.
3) The panel receives the event, shrugs its shoulders and passes it on to its parent, the frame.
4) The frame gets the event, says 'Aha!' and passes it to its OnAddClick() method.
This works because button-clicks produce wxCommandEvents, and these propagate to parent controls. Try the same thing with a mouse-click, and it would fail: wxMouseEvent doesn't propagate.