How to generate an event -- the answer

This is an answer to myself (original message date 12 Apr 2003 21:38:40
+0100)

The problem was me wanting to generate an wxEVT_COMMAND_LISTBOX_SELECT
event by hand.

My documentation does not say that wxListBox inherits from wxControl,
but it does (or it appears to). This appears to be a documentation bug.

So, the method wxListBox.Command exists. The docs on wxControl say that
wxControl::wxCommand is like
void Command(wxCommandEvent& event)

The next problem is to generate an wxCommandEvent instance. The docs
say that the constructor of wxCommandEvent is like
wxCommandEvent(WXTYPE commandEventType = 0, int id = 0)

Unfortunately, the wxPython seems do not provide handy WXTYPE command
event types, which are not documented too. This may be both a
documentation bug and a wxPython implementation bug.

So we need to find the command type by producing it and printing the
type, with a test program with code like this:

    def OnSelect(self, event):
        print event.GetEventType()

With this, I found that the type of the EVT_LISTBOX event is 10004.

Finnaly, it looks like the id parameter in wxCommandEvent needs to be
set to the id of the wxListBox instance.

All of the above put together, the line that produces an EVT_LISTBOX
event is:

    self.Command(wx.wxCommandEvent(10004, self.GetId()))

where 'self' is the wxListBox instance.

Hope that this terse recipe helps someone...

Sebrosa

Jose' Sebrosa wrote:

This is an answer to myself (original message date 12 Apr 2003 21:38:40
+0100)

The problem was me wanting to generate an wxEVT_COMMAND_LISTBOX_SELECT
event by hand.

My documentation does not say that wxListBox inherits from wxControl,
but it does (or it appears to). This appears to be a documentation bug.

So, the method wxListBox.Command exists. The docs on wxControl say that
wxControl::wxCommand is like void Command(wxCommandEvent& event)

The next problem is to generate an wxCommandEvent instance. The docs
say that the constructor of wxCommandEvent is like
wxCommandEvent(WXTYPE commandEventType = 0, int id = 0)

Unfortunately, the wxPython seems do not provide handy WXTYPE command
event types, which are not documented too. This may be both a
documentation bug and a wxPython implementation bug.

WXTYPE (wxEventType actually) is currently just a typedef for int.

So we need to find the command type by producing it and printing the
type, with a test program with code like this:

    def OnSelect(self, event):
        print event.GetEventType()

With this, I found that the type of the EVT_LISTBOX event is 10004.

The event types are generated dynamically, so you can't depend on this value not changing across versions and platforms. Why not just use wxEVT_COMMAND_LISTBOX_SELECTED? That's what EVT_LISTBOX uses to connect the event to the handler, so that's what you want to use to send it too.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!