PDA

View Full Version : Solved: Navigate Controls



tqm1
06-28-2007, 10:56 PM
Dear Experts

While using Control Toolbox, I insert three textboxes on sheet1

There is problem sending curosor from one to other textbox

When cursor is in Textbox1 and I press Enter or Tab key then cursor must move to textbox2

When cursor in in Textbox2 and I press Left arrow key then cursor must move back to textbox1

Please help how to navigate controls with Enter key, Tab key and Arrow keys.

Thank

geekgirlau
06-28-2007, 11:44 PM
AFAIK you can't do this (although I'm sure someone will correct me if I'm wrong :devil2: ).

The text box does have a KeyPress event, however Tab, Enter and arrow keys do not trigger this event.

Personally, I seldom use text boxes on a sheet because of this very reason - in most cases you can simply unlock specific cells and then protect the sheet, enabling you to Tab and Shift-Tab through those cells.

rbrhodes
06-29-2007, 03:53 PM
Hi tqm1 and geekgitlau,

Yes it can be done, this code is based on this post by Andy Pope:

http://www.ozgrid.com/forum/showthread.php?t=47454

It recognizes Tab and Enter as forward and Left arrow as back. Note it loops from 1 to 3 as well.




Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = vbKeyLeft Or Shift = 1 And KeyCode = vbKeyTab Then
TextBox3.Activate
ElseIf KeyCode = vbKeyTab Or KeyCode = vbKeyReturn Or KeyCode = vbKeyRight Then
TextBox2.Activate
End If
End Sub
Private Sub TextBox2_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = vbKeyLeft Or Shift = 1 And KeyCode = vbKeyTab Then
TextBox1.Activate
ElseIf KeyCode = vbKeyTab Or KeyCode = vbKeyReturn Or KeyCode = vbKeyRight Then
TextBox3.Activate
End If
End Sub
Private Sub TextBox3_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = vbKeyLeft Or Shift = 1 And KeyCode = vbKeyTab Then
TextBox2.Activate
ElseIf KeyCode = vbKeyTab Or KeyCode = vbKeyReturn Or KeyCode = vbKeyRight Then
TextBox1.Activate
End If
End Sub



Cheers,

dr

Edit: Added Shift+Tab code. Hi Ivan!

Ivan F Moala
06-29-2007, 04:03 PM
you need to code for this to happen

eg


Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)

If KeyCode = 13 Then
With Me
.TextBox2.Activate
End With
End If

End Sub

Ivan F Moala
06-29-2007, 04:04 PM
sorry, was posting when rbrhodes was :)