在wxFrame - C ++ / wxWidgets处理来自wxTextCtrl的事件 [英] Handling events from wxTextCtrl on wxFrame - C++/wxWidgets
问题描述
我有一个 MyFrame
,源自 wxFrame
。将 wxTextCtrl
添加到此框架。我可以在框架中处理这个文本控件的 EVT_KEY_DOWN
吗?类似
BEGIN_EVENT_TABLE(wxTextCtrl,wxControl)
EVT_KEY_DOWN(MyFrame :: OnKeyDown)
END_EVENT_TABLE )
上面的代码似乎不工作。文档说这样的事件只能由事件发起的对象来处理。所以我应该子类 wxTextCtrl
来处理这个,并以某种方式发送信息到框架?
什么是最好的方法 c> wxCommandEvent 和
来自子控件的wxNotifyEvent 类型事件被设置为自动向上传播到父框架。但是, wxKeyEvent
是从 wxEvent
派生的,因此它不会传播到父框架。那么,您可以使用动态事件处理程序将一些事件路由到任何 wxEvtHandler
派生对象。
wxWidgets 2.8,你应该使用 wxEvtHandler :: Connect
。此方法在此处中有所描述。您还可以查看此示例代码。
在wxWidgets 2.9和SVN下,您应该使用 wxEvtHandler :: Bind<>
:
MyFrame :: MyFrame(...)
{
m_textcontrol-> Bind(wxEVT_KEY_DOWN,& MyFrame :: OnTextControlKeyDown,this);
}
wxEvtHandler :: Bind<>
方法在此处中有所描述。
I have a MyFrame
which derives from wxFrame
. A wxTextCtrl
is added to this frame. Can I handle EVT_KEY_DOWN
of this text control in frame? Something like,
BEGIN_EVENT_TABLE(wxTextCtrl, wxControl)
EVT_KEY_DOWN(MyFrame::OnKeyDown)
END_EVENT_TABLE()
The above code seems to be not working. Documentation says events like this can only be handled by the object where the event is originated. So should I subclass wxTextCtrl
to handle this and somehow send the information to frame?
What is the best way of doing this?
The wxCommandEvent
and wxNotifyEvent
type events from a child controls are set to propagate upward to the parent frame automatically. However, the wxKeyEvent
is derived from the wxEvent
so it is not propagating to the parent frame. Well, you may use dynamic event handlers to route some of the events to any of wxEvtHandler
derived objects.
Under wxWidgets 2.8 you should use wxEvtHandler::Connect
. This method is described here. You may also look at this sample code.
Under wxWidgets 2.9 and SVN you should use wxEvtHandler::Bind<>
:
MyFrame::MyFrame(...)
{
m_textcontrol->Bind(wxEVT_KEY_DOWN, &MyFrame::OnTextControlKeyDown, this);
}
The wxEvtHandler::Bind<>
method is described here.
这篇关于在wxFrame - C ++ / wxWidgets处理来自wxTextCtrl的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!