[wxMSW,WXGTK] Modeless dialog, how to handle OK event
Carsten A. Arnholm
arnholm at offline.no
Sat Sep 29 16:40:48 PDT 2007
Vadim Zeitlin wrote:
> On Sun, 30 Sep 2007 00:12:46 +0200 "Carsten A. Arnholm"
> <arnholm at offline.no> wrote:
>
>> What is the proper way to handle OK being pushed, so a different
>> action from what happens when Cancel is pushed can be taken?
>>
>> I have done this in MyDialog
>> Connect(wxID_OK,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&MyDialog::OnOk);
>>
>> And it seems to work. But I wonder if this is the proper way.
>
> I'm afraid I don't understand what exactly you're looking for. You
> can, of course, handle the events from the button like above (although you
> should use wxCommandEventHandler macro instead of casting to
> wxObjectEventFunction like this). Or you can use an event table in the
> dialog class, which may be more usual.
Thank you for replying, Vadim.
I am trying to understand how to best handle some common patterns, in this
case modeless dialogs. Such patterns are probably obvious to seasoned
wxWidgets programmers, but I am still learning. Most things in wxWidgets
appear very clear & concise, and fairly easy to understand. But I have been
confused about some simple things, e.g. how to handle OK and Cancel in
dialogs, but it is getting a bit clearer now.
I followed your advice and fund I could do
Connect(wxID_OK,wxEVT_COMMAND_BUTTON_CLICKED,wxCommandEventHandler(MyDialog::OnOk));
and similar using wxMouseEventHandler for mouse events. I had not seen this
before, so thank you. I agree it is much cleaner. I did the casting, because
that is what wxSmith does in Code::Blocks.
Now, based on your comment I also found I could do
BEGIN_EVENT_TABLE(...)
EVT_COMMAND(wxID_OK,wxEVT_COMMAND_BUTTON_CLICKED,MyDialog::OnOk)
END_EVENT_TABLE()
After looking at it, it seems obvious. Part of the problem here is pieceing
together all the available information. I knew about event tables, I have
read the wxWidgets book etc.. Still, somehow it was not obvious to me what
to do with a call like CreateButtons(wxOK|wxCANCEL); wrt. handling the
event. You have to understand that wxOK isn't wxID_OK of example, it took me
a while.
So I think your replied to my concrete question was useful, the best would
here be to use the event table I guess. But it was also useful to learn
about better ways of using Connect(...), since it is sometimes necessary to
install events at runtime (I have seen it with some mouse events).
Still learning, thanks for your time :-)
--
Carsten A. Arnholm
http://arnholm.org/
N59.776 E10.457
More information about the wx-users
mailing list