The code you posted should work, the issue must be elswhere.
FWIW, this works as expected
MyDialog.h
Code: Select all
#pragma once
#include <wx/wx.h>
wxDECLARE_EVENT(TASK_INSERTED, wxCommandEvent);
class MyDialog : public wxDialog
{
public:
MyDialog(wxWindow* parent);
protected:
void OnAddTask(wxCommandEvent&);
};
MyDialog.cpp
Code: Select all
#include "MyDialog.h"
MyDialog::MyDialog(wxWindow* parent)
: wxDialog(parent, wxID_ANY, "Add Task Dialog")
{
wxBoxSizer* mainSizer = new wxBoxSizer(wxVERTICAL);
wxButton* button = new wxButton(this, wxID_ANY, "Add Task");
button->Bind(wxEVT_COMMAND_BUTTON_CLICKED, &MyDialog::OnAddTask, this);
mainSizer->Add(button, wxSizerFlags().Expand().DoubleBorder());
mainSizer->Add(CreateStdDialogButtonSizer(wxOK | wxCANCEL), wxSizerFlags().Expand().DoubleBorder());
SetSizerAndFit(mainSizer);
}
void MyDialog::OnAddTask(wxCommandEvent&)
{
wxCommandEvent event(TASK_INSERTED, GetId());
event.SetEventObject(this);
GetParent()->ProcessWindowEvent(event);
}
MyFrame.cpp
Code: Select all
#include <wx/wx.h>
#include "MyDialog.h"
wxDEFINE_EVENT(TASK_INSERTED, wxCommandEvent);
class MyFrame : public wxFrame
{
public:
MyFrame() : wxFrame(nullptr, wxID_ANY, "Test", wxDefaultPosition, wxSize(800, 600))
{
wxPanel* mainPanel = new wxPanel(this);
wxBoxSizer* mainPanelSizer = new wxBoxSizer(wxVERTICAL);
wxButton* button = new wxButton(mainPanel, wxID_ANY, "Show Dialog...");
button->Bind(wxEVT_COMMAND_BUTTON_CLICKED, &MyFrame::OnShowDialog, this);
mainPanelSizer->Add(button, wxSizerFlags().Expand().DoubleBorder());
wxTextCtrl* logCtrl = new wxTextCtrl(mainPanel, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize,
wxTE_MULTILINE | wxTE_READONLY | wxTE_RICH2);
wxLog::SetActiveTarget(new wxLogTextCtrl(logCtrl));
mainPanelSizer->Add(logCtrl, wxSizerFlags().Expand().DoubleBorder().Proportion(5));
mainPanel->SetSizer(mainPanelSizer);
Bind(TASK_INSERTED, &MyFrame::OnAddTask, this);
}
protected:
void OnShowDialog(wxCommandEvent&)
{
MyDialog dlg(this);
dlg.ShowModal();
}
void OnAddTask(wxCommandEvent&)
{
wxLogMessage("Task added.");
}
};
class MyApp : public wxApp
{
public:
bool OnInit() override
{
(new MyFrame)->Show();
return true;
}
}; wxIMPLEMENT_APP(MyApp);;
Not sure whether something like this, i.e., passing the event from the modal dialog, is a good idea....