使用事件表处理自定义对话框事件

Custom Dialog Event Handling with Event Tables

我假装 "grab" 并处理事件:来自 wxTextCtrl 的 EVT_TEXT 和来自 wxID_APPLY 的 EVT_BUTTON。

我尝试使用事件表处理事件,但我不能?为什么?

源代码:

#include <wx/wxprec.h>
#ifndef WX_PRECOMP
   #include <wx/wx.h>
#endif

class Dialog : public wxDialog
{
    public:
        Dialog(wxWindow *parent, const wxString &title);

    private:
        void OnNameChange(wxCommandEvent &event);
        void OnApply(wxCommandEvent &event);

        wxDECLARE_EVENT_TABLE();

        wxTextCtrl *m_name;
};

enum
{
    ID_NAME = 1
};

Dialog::Dialog(wxWindow *parent, const wxString &title) :
    wxDialog(parent, wxID_ANY, title, wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER)
{
    wxBoxSizer *vbox = new wxBoxSizer(wxVERTICAL);
    SetSizer(vbox);

    wxFlexGridSizer *flexGrid = new wxFlexGridSizer(2, 2, 5, 5);

    wxStaticText *label = new wxStaticText(this, wxID_ANY, "&Name:");
    flexGrid->Add(label);

    m_name = new wxTextCtrl(this, ID_NAME, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxTE_PROCESS_ENTER);
    flexGrid->Add(m_name, 1, wxEXPAND);

    label = new wxStaticText(this, wxID_ANY, "&Description:");
    flexGrid->Add(label);

    m_description = new wxTextCtrl(this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE);
    flexGrid->Add(m_description, 1, wxEXPAND);

    flexGrid->AddGrowableRow(1, 1);
    flexGrid->AddGrowableCol(1, 1);

    vbox->Add(flexGrid, 1, wxALL | wxEXPAND, 15);

    vbox->Add(CreateSeparatedButtonSizer(wxAPPLY | wxCANCEL), 0, wxEXPAND | wxALL, 5);  
}

void Dialog::OnNameChange(wxCommandEvent &event)
{
    m_description->AppendText("Hello\n");
}

void Dialog::OnApply(wxCommandEvent &event)
{
    m_description->AppendText("Apply\n");
}

wxBEGIN_EVENT_TABLE(Dialog, wxDialog)
    EVT_TEXT(ID_NAME, Dialog::OnNameChange)
    EVT_BUTTON(wxID_APPLY, Dialog::OnApply)
wxEND_EVENT_TABLE()

只能动态处理事件吗?

Bind(wxEVT_TEXT, &Dialog::OnNameChange, this, m_name->GetId());

显示的代码绝对应该有效。它不是独立的,所以我无法对其进行测试,但一定有其他未在此处显示的东西阻止它工作。如果您仍然无法自行解决问题,请尝试制作SSCCE