Custom events passing values back to controls Topic is solved

Are you writing your own components and need help with how to set them up or have questions about the components you are deriving from ? Ask them here.
Post Reply
imekon
Earned a small fee
Earned a small fee
Posts: 10
Joined: Tue Jan 16, 2007 3:41 pm

Custom events passing values back to controls

Post by imekon » Thu Jan 18, 2007 5:12 pm

As far as I can see, events can inform other objects about details etc.

How would I write an event that a control issues that expects a value to be returned?

I tried the following:

Code: Select all

class wxNoteCountEvent : public wxEvent
{
public:
    wxNoteCountEvent( const wxNoteCountEvent &original );
    wxNoteCountEvent( wxEventType commandType = wxEVT_NULL, int id = 0 );

    virtual wxEvent *Clone() const { return new wxNoteCountEvent(*this); }

    void SetCount(int value) { count = value; }
    int GetCount() const { return count; }

private:
    int count;
};
In the control, I do the following:

Code: Select all

int wxPianoRoll::GetNoteCount()
{
    wxNoteCountEvent event( wxEVT_NOTE_COUNT, GetId() );

    event.SetEventObject( this );

    GetEventHandler()->ProcessEvent(event);

    return event.GetCount();
}
So the event is updated by the parent control which sets the count as in:

Code: Select all

BEGIN_EVENT_TABLE(MainFrame, wxFrame)
	EVT_MENU(ID_QUIT, MainFrame::OnQuit)
	EVT_MENU(ID_ABOUT, MainFrame::OnAbout)
	EVT_NOTE_COUNT(ID_PIANOROLL, MainFrame::OnNoteCount)
END_EVENT_TABLE()

void MainFrame::OnNoteCount(wxNoteCountEvent &event)
{
    event.SetCount(100);
}
However, the value the gets returned the custom control is always 0, which suggests events don't work that way.

DavidHart
Site Admin
Site Admin
Posts: 4008
Joined: Thu Jan 12, 2006 6:23 pm
Location: IoW, UK

Post by DavidHart » Thu Jan 18, 2007 6:52 pm

Hi,

You didn't show your copy constructor implementation, but it needs to contain the line:
this->SetCount( original.GetCount() );

Regards,

David

clyde729
Super wx Problem Solver
Super wx Problem Solver
Posts: 426
Joined: Mon May 29, 2006 10:50 pm
Location: Jena, Germany

Post by clyde729 » Thu Jan 18, 2007 6:53 pm

You have to derive (at least) from wxCommandEvent (or wxNotifyEvent), if you want to catch the event in the frame.

Only wxCommandEvent and its derivates are allowed to climb up the window hierarchy. Since your event only derives from wxEvent, it won't get catched. If you wan't to use it in that way, you have to use "Connect" (wxEvtHandler) on your wxPianoRoll and use your Frame instance as eventSink (last parameter).
OS: Windows XP Home, Compiler: MingW, Version: wxWidgets 2.8.0, IDE: wx-Devcpp

imekon
Earned a small fee
Earned a small fee
Posts: 10
Joined: Tue Jan 16, 2007 3:41 pm

Post by imekon » Thu Jan 18, 2007 8:49 pm

DavidHart wrote:Hi,

You didn't show your copy constructor implementation, but it needs to contain the line:
this->SetCount( original.GetCount() );

Regards,

David
This is the copy constructor:

Code: Select all

wxNoteCountEvent::wxNoteCountEvent( const wxNoteCountEvent &original )
{
    count = original.count;
}
Still didn't help though.

imekon
Earned a small fee
Earned a small fee
Posts: 10
Joined: Tue Jan 16, 2007 3:41 pm

Post by imekon » Thu Jan 18, 2007 8:52 pm

clyde729 wrote:You have to derive (at least) from wxCommandEvent (or wxNotifyEvent), if you want to catch the event in the frame.

Only wxCommandEvent and its derivates are allowed to climb up the window hierarchy. Since your event only derives from wxEvent, it won't get catched. If you wan't to use it in that way, you have to use "Connect" (wxEvtHandler) on your wxPianoRoll and use your Frame instance as eventSink (last parameter).
That was it!

Deriving from wxCommandEvent and hey presto I now get the value set in MainFrame passed back.

Thank you!

vdell
Moderator
Moderator
Posts: 536
Joined: Fri Jan 07, 2005 3:44 pm
Location: Finland
Contact:

Post by vdell » Fri Jan 19, 2007 4:43 am

imekon wrote:
clyde729 wrote:You have to derive (at least) from wxCommandEvent (or wxNotifyEvent), if you want to catch the event in the frame.

Only wxCommandEvent and its derivates are allowed to climb up the window hierarchy. Since your event only derives from wxEvent, it won't get catched. If you wan't to use it in that way, you have to use "Connect" (wxEvtHandler) on your wxPianoRoll and use your Frame instance as eventSink (last parameter).
That was it!

Deriving from wxCommandEvent and hey presto I now get the value set in MainFrame passed back.

Thank you!
FYI, this would be same as deriving from wxEvent and doing

Code: Select all

wxEvent::m_propagationLevel = wxEVENT_PROPAGATE_MAX;
in your derived constructor.
Visual C++ 9.0 / Windows XP Pro SP3 / wxWidgets 2.9.0 (SVN) | Colligere

imekon
Earned a small fee
Earned a small fee
Posts: 10
Joined: Tue Jan 16, 2007 3:41 pm

Post by imekon » Fri Jan 19, 2007 9:38 am

vdell wrote:
FYI, this would be same as deriving from wxEvent and doing

Code: Select all

wxEvent::m_propagationLevel = wxEVENT_PROPAGATE_MAX;
in your derived constructor.
This didn't work - perhaps more settings need to change.

clyde729
Super wx Problem Solver
Super wx Problem Solver
Posts: 426
Joined: Mon May 29, 2006 10:50 pm
Location: Jena, Germany

Post by clyde729 » Fri Jan 19, 2007 1:25 pm

Your implementation on the copy-ctor isn't complete. It must look like:

Code: Select all

wxNoteCountEvent::wxNoteCountEvent( const wxNoteCountEvent &original )
: wxCommandEvent(original)
{
    count = original.count;
}


You have to call the baseclass - version of the copy-ctor in order to copy things like eventobject or eventtype (propagation-level, too).
OS: Windows XP Home, Compiler: MingW, Version: wxWidgets 2.8.0, IDE: wx-Devcpp

imekon
Earned a small fee
Earned a small fee
Posts: 10
Joined: Tue Jan 16, 2007 3:41 pm

Post by imekon » Fri Jan 19, 2007 4:10 pm

clyde729 wrote: You have to call the baseclass - version of the copy-ctor in order to copy things like eventobject or eventtype (propagation-level, too).
You're right... I had a feeling I was missing something when I wrote it.

Post Reply