本文介绍了在wxFrame - C ++ / wxWidgets处理来自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的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 07:13