# Timing issue with set\_focus?

**URL:** <https://www.ruby-forum.com/t/timing-issue-with-set-focus/167698>\
**Category:** wxRuby\
**Created:** [May 11, 2009, 6:14pm UTC](https://www.ruby-forum.com/t/timing-issue-with-set-focus/167698 "2009-05-11T18:14:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Phil\_Puccio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5fc32e/32.png) [@Phil\_Puccio](https://www.ruby-forum.com/u/Phil_Puccio)\
**Post date:** [May 11, 2009, 6:14pm UTC](https://www.ruby-forum.com/t/timing-issue-with-set-focus/167698/1 "2009-05-11T18:14:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Phil\_Puccio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5fc32e/32.png) [@Phil\_Puccio](https://www.ruby-forum.com/u/Phil_Puccio)\
**Post date:** [May 11, 2009, 10:42pm UTC](https://www.ruby-forum.com/t/timing-issue-with-set-focus/167698/2 "2009-05-11T22:42:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Phil\_Puccio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5fc32e/32.png) [@Phil\_Puccio](https://www.ruby-forum.com/u/Phil_Puccio)\
**Post date:** [May 14, 2009, 2:07pm UTC](https://www.ruby-forum.com/t/timing-issue-with-set-focus/167698/3 "2009-05-14T14:07:02Z")

</div>

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
