Forum: wxRuby Timing issue with set_focus?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
2d62fcd79dd868c8be4f05d88d7f98e2?d=identicon&s=25 Phil Puccio (Guest)
on 2009-05-11 18:14
(Received via mailing list)
Hi,

I am using WxRuby to build a WinXP desktop application.  I've just hit a
problem when trying to use set_focus.  When a user selects a row in a
ListCtrl, I want to set focus to a particular TextCtrl.  So in the
handler
for the ListCtrl's evt_list_item_selected event, I get hold of the
TextCtrl
and send it set_focus.

I can see the TextCtrl get focus only for a split-second (the text
insertion
bar briefly appears, then disappears).  I suspect that my ListCtrl event
handler is called (and I move focus) before the ListCtrl is completely
finished handling its own event.  So right after I move focus to the
TextCtrl, the ListCtrl grabs it back.

Is there a technique for getting it to behave the way I want it to?
I've
tried doing the set_focus in another thread after waiting 2 seconds, but
the
behavior is exactly the same as when I do the set_focus in the event
handler
thread.

Thanks for any advice you can provide.

Phil
3396e4a3df8a840faec520af8555a400?d=identicon&s=25 Mario Steele (Guest)
on 2009-05-11 22:42
(Received via mailing list)
Hello Phil,

Welcome to the list, and it's good that you are finding wxRuby useful to
your purposes.  As to your question, you are correct in assuming that
ListCtrl's event handling isn't completed when you attempt to switch
focus
from the ListCtrl to your TextCtrl.  There are two ways in which you can
do
this, but you have to understand how Ruby and wxWidgets are handled on
the
back side of the system.

The first method, is similar to what you was attempting to do, however
kinda
offset.  When you attempted to switch focus from another thread, the
context
still remained, so it didn't quite fit what you was trying to do.
Instead,
try doing this:

Wx::Timer.after(500) do
  # set_focus your TextCtrl here
end

This launches a timer, that will only execute once, and will do it after
500
milliseconds.  That should give wxWidgets enough time to finish it's
processing of the current event, and then allow you to move your focus
to
another control.

The second method in which to do this, is to go ahead and do your normal
set_focus, and once your done processing the event, and when you are
completed with it, simply do an evt.veto, which will prevent any further
processing of the event pass your handler.

hth,

Mario
2d62fcd79dd868c8be4f05d88d7f98e2?d=identicon&s=25 Phil Puccio (pooch)
on 2009-05-14 14:07
Thanks, Mario.

Yes, I'm really enjoying using wxRuby.  I've been away from desktop apps
for a very long time, and after a few years of web apps, it's been fun
to quickly develop a nice, fast app using Ruby and native widgets.

I used your Wx::Timer suggestion


Mario Steele wrote:
> Hello Phil,
>
> Welcome to the list, and it's good that you are finding wxRuby useful to
> your purposes.  As to your question, you are correct in assuming that
> ListCtrl's event handling isn't completed when you attempt to switch focus
> from the ListCtrl to your TextCtrl.  There are two ways in which you can do
> this, but you have to understand how Ruby and wxWidgets are handled on the
> back side of the system.
>
> ...snip...
>
> Wx::Timer.after(500) do
>   # set_focus your TextCtrl here
> end
>
This topic is locked and can not be replied to.