简体   繁体   English

如何在功能上设置多个事件

[英]How to set multiple events on function

How do you call a function that allready has an event attached to it? 您如何调用已附加事件的函数?

I want to be able to call the function with something like this: 我希望能够使用如下所示的函数来调用该函数:

statsDialog::statsDialog(..) : wxDialog(..) {
    updateStats();
}

Or, how to bind multiple eventhandlers to a function? 或者,如何将多个事件处理程序绑定到一个函数? For example call updateStats with the wxEVT_SET_FOCUS event AND when UPDATE_STATS_BUTTON is pressed. 例如调用与updateStats wxEVT_SET_FOCUS事件和UPDATE_STATS_BUTTON被按下。 How can I do this without duplicating code (copying the updateStats function just changing it to wxFocusEventHandler )? 如何在不复制代码的情况下做到这一点(复制updateStats函数,仅将其更改为wxFocusEventHandler )?

BEGIN_EVENT_TABLE(statsDialog, wxDialog)
EVT_BUTTON(UPDATE_STATS_BUTTON, statsDialog::updateStats)
END_EVENT_TABLE()

statsDialog::statsDialog(..) : wxDialog(..) {    
    // layout stuff
}

void
statsDialog::updateStats(wxCommandEvent& event) {
    // do stuff on dialog focus AND when UPDATE_STATS_BUTTON is pressed
}

Simply have a function called DoUpdateStats() called from both the focus and button event handlers. 只需从焦点和按钮事件处理程序中调用一个名为DoUpdateStats()的函数即可。 This is particularly trivial when using C++11 with Bind() : 当将C ++ 11与Bind()一起使用时,这尤其琐碎:

Bind(wxEVT_SET_FOCUS, [](wxFocusEvent& e) { e.Skip(); DoUpdateStats(); });
btn->Bind(wxEVT_BUTTON, [](wxCommandEvent&) { DoUpdateStats(); });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM