# Focus on TextCtrl

**URL:** <https://www.ruby-forum.com/t/focus-on-textctrl/125963>\
**Category:** wxRuby\
**Created:** [January 14, 2008, 9:31am UTC](https://www.ruby-forum.com/t/focus-on-textctrl/125963 "2008-01-14T09:31:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![antonk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a5b964/32.png) [@antonk](https://www.ruby-forum.com/u/antonk)\
**Post date:** [January 14, 2008, 9:31am UTC](https://www.ruby-forum.com/t/focus-on-textctrl/125963/1 "2008-01-14T09:31:47Z")

</div>

Hello.  
I have a problem with set focus on a TextCtrl in wxRuby.  
I must do something like  
Login TextCtrl and set focus on a next TextCtrl by clicking Tab key.

---

<div class="post-metadata">

**Author:** ![antonk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a5b964/32.png) [@antonk](https://www.ruby-forum.com/u/antonk)\
**Post date:** [January 14, 2008, 10:23am UTC](https://www.ruby-forum.com/t/focus-on-textctrl/125963/2 "2008-01-14T10:23:28Z")

</div>

I’m find something like this.  
somebody have a same problem

If I create a window containing two TextCtrl widgets, enter text in  
one, and press the tab key, focus should move to the other one. That  
doesn’t happen automatically. Is there some configuration I have to do  
to make that happen?  
maybe it will more clear to explain

---

<div class="post-metadata">

**Author:** ![antonk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a5b964/32.png) [@antonk](https://www.ruby-forum.com/u/antonk)\
**Post date:** [January 14, 2008, 10:49am UTC](https://www.ruby-forum.com/t/focus-on-textctrl/125963/3 "2008-01-14T10:49:39Z")

</div>

Here is a sample code for this problem  
…  
@login\_field = Wx::TextCtrl.new(@loginokno, -1, ‘’,Wx::Point.new(30,60),  
Wx::Size.new(150,25))  
@haslo\_field = Wx::TextCtrl.new(@loginokno, -1,  
‘’,Wx::Point.new(30,110), Wx::Size.new(150,25))  
@lastname\_field = Wx::TextCtrl.new(@loginokno, -1,  
‘’,Wx::Point.new(30,160), Wx::Size.new(150,25))  
@login\_field.set\_focus  
@login\_field.evt\_key\_down { |evt| on\_log\_key\_down(evt) }  
@haslo\_field.evt\_key\_down { |evt| on\_pas\_key\_down(evt) }  
…  
def on\_log\_key\_down(evt)  
keycode = evt.get\_key\_code()  
if (keycode == 9)  
@haslo\_field.set\_focus  
else  
evt.skip()  
end  
end

def on\_pas\_key\_down(evt)  
keycode = evt.get\_key\_code()  
if (keycode == 9)  
@lastname\_field.set\_focus  
else  
evt.skip()  
end  
end

---

<div class="post-metadata">

**Author:** ![antonk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a5b964/32.png) [@antonk](https://www.ruby-forum.com/u/antonk)\
**Post date:** [January 14, 2008, 10:51am UTC](https://www.ruby-forum.com/t/focus-on-textctrl/125963/4 "2008-01-14T10:51:36Z")

</div>

Hi

Anton K. wrote:

> If I create a window containing two TextCtrl widgets, enter text in  
> one, and press the tab key, focus should move to the other one. That  
> doesn’t happen automatically. Is there some configuration I have to do  
> to make that happen?  
> Arrange the TextCtrl widgets within a Wx::Panel, and then tabbing should  
> work automatically. Panel organises a group of widgets so that the  
> operating system knows which to tab between. For example:

* * *

require ‘wx’

Wx::App.run do  
frame = Wx::Frame.new(nil, :title =\> ‘text tab’)  
panel = Wx::Panel.new(frame)  
txt1 = Wx::TextCtrl.new(panel, :pos =\> [10, 10])  
txt2 = Wx::TextCtrl.new(panel, :pos =\> [10, 50])  
frame.show  
end

* * *

Using Wx::Panel is also recommended because it ensures the frame has the  
right background colour, esp on Windows.

alex

---

<div class="post-metadata">

**Author:** ![antonk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a5b964/32.png) [@antonk](https://www.ruby-forum.com/u/antonk)\
**Post date:** [January 14, 2008, 11:23am UTC](https://www.ruby-forum.com/t/focus-on-textctrl/125963/5 "2008-01-14T11:23:36Z")

</div>

Thank you very match.  
your code sample is shorter and changing focus with Tab key press is  
really working:)
