I have created a working custom event with the help of the wiki.
Now I am trying the following:
I have 3 sets of header and source. A GUI set, Main app frame set, and a derived frame set.
What I am trying to do is trigger a custom event in the derived frame,
and have it picked up in the parent (Main frame) in order to execute a function there.
I am working on Windows 10 (latest version), Code::Blocks 20.03, mingw with gcc 11.2.0, and FrameBuilder.
The GUI has 2 classes: GUIframe and MyFrame2.
Code: Select all
GUIFrame( wxWindow* parent, wxWindowID id = wxID_ANY, const wxString& title = wxT("Frame 1"), const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxSize( 481,466 ), long style = wxDEFAULT_FRAME_STYLE|wxTAB_TRAVERSAL );
MyFrame2( wxWindow* parent, wxWindowID id = wxID_ANY, const wxString& title = wxT("Frame 2"), const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxSize( 500,300 ), long style = wxDEFAULT_FRAME_STYLE|wxTAB_TRAVERSAL );
The Main Frame looks as follows:
It has 1 button to trigger the custom event, and a function that responds to the event by showing text in a staticTextCtrl.
Code: Select all
enum
{
evtCustom_1 = 1000
};
wxDEFINE_EVENT(EVT_CUSTOM_1, wxCommandEvent);
wxCommandEvent evt_1(EVT_CUSTOM_1, evtCustom_1);
Fun2Test3Frame::Fun2Test3Frame(wxFrame *frame) : GUIFrame(frame)
{
Bind(EVT_CUSTOM_1, &Fun2Test3Frame::TestEvent1, this, evtCustom_1);
}
void Fun2Test3Frame::m_button2OnButtonClick( wxCommandEvent& event )
{
evt_1.SetString("Triggered from Frame 1");
wxPostEvent(this, evt_1);
}
void Fun2Test3Frame::TestEvent1(wxCommandEvent &event)
{
m_staticText2->SetLabelText(event.GetString());
}
The derived frame looks as follows:
It also has 1 button to trigger the custom event, and a function that responds to the event by showing text in a staticTextCtrl.
Code: Select all
enum
{
evtCustom_2 = 1000
};
wxDEFINE_EVENT(EVT_CUSTOM_2, wxCommandEvent);
wxCommandEvent evt_2(EVT_CUSTOM_2, evtCustom_2);
Frame2::Frame2( wxWindow* parent ) : MyFrame2( parent )
{
Bind(EVT_CUSTOM_2, &Frame2::TestEvent2, this, evtCustom_2);
}
void Frame2::m_button3OnButtonClick( wxCommandEvent& event )
{
evt_2.SetString("Triggered From Frame 2");
wxPostEvent(this, evt_2);
wxWindow *prnt = GetParent();
if(prnt != NULL)
{
wxPostEvent(prnt, evt_2);
// GetParent()->GetEventHandler()->ProcessEvent(evt_2);
}
}
void Frame2::TestEvent2(wxCommandEvent &event)
{
m_staticText3->SetLabelText("Frame2 TestEvent2 triggered");
}
I cannot make the second one work (catch the event and trigger execution of TestEvent1 in the Main Frame).
I gave evtCustom_1 and evtCustom_2 the same id, thinking that the id_name does not matter, but its value does.
Giving it different values made no difference.
The event is triggered by the value, not the name right?
prnt->GetLabel() returns the title of the Main frame ("Frame 1"), so this looks good.
According to the wiki (and several posts I found), this should work.
Have I missed something important here?
I appreciate your help to make it work. Thank you.