Calling event handler directly

后端 未结 2 1608
渐次进展
渐次进展 2021-01-25 09:04

Having trouble to call an event handler directly from my code. I found the same question 2 years ago here. original question

But the line me_InsertCommentText(wxCo

2条回答
  •  -上瘾入骨i
    2021-01-25 10:04

    The answer about using a named temporary variable is technically correct, but the important thing is that you really shouldn't be doing this in the first place. The handlers are only supposed to be called from wxWidgets and instead of calling some OnFoo(wxFooEvent&) directly you should refactor your code to just call some new DoFoo() from OnFoo() and then call DoFoo() from the rest of your code if you need it.

    This becomes even simpler when using C++11 as you don't even need to have OnFoo() at all in this case and could just write

    whatever->Bind(wxEVT_FOO, [=](wxCommandEvent&) { DoFoo(); });
    

    to avoid an extra function.

提交回复
热议问题