Timing issue with set_focus?

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

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

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 S. 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