We help IT Professionals succeed at work.

How do I hard code the next control in the tab order to get focus?

IvanHowarth
IvanHowarth asked
on
225 Views
Last Modified: 2010-04-23
The title pretty much sums up my quest.

The following is a code snippet as an example. I want this key trapping method, when the return key is pressed, and an entry has been made, automatically jumps to the next tabbed control. As this event handler is assigned to multiple controls (for code efficiency), I can't specifically name the actual control I want next to get focus (as it depends which control fired the event).

Is my request possible? The line of code which starts with ??? is where I'm struggling with.

Private Sub AllTextBoxes_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtBox1.KeyPress, txtBox2.KeyPress

        Dim txtField As TextBox = CType(sender, TextBox)
        Dim KeyAscii As Integer = Asc(e.KeyChar)
        Select Case KeyAscii
            Case Asc(ControlChars.Back), Asc("0") To Asc("9") 'Allow
                e.Handled = False
            Case Asc(".")
                If InStr(txtField.Text, ".") = 0 Then
                    e.Handled = False
                Else
                    e.Handled = True
                End If
            Case Asc(ControlChars.Cr) 'Upon return
                txtField.Text.Trim()
                If Len(txtField.Text) > 0 Then                 'If something entered, jump to next control
                    ???[jump to next tabbed control].Focus()
                End If
                e.Handled = True
                    ???[jump to next tabbed control].Focus()
            Case Else
                e.Handled = True
        End Select
    End Sub

Please can you help?
Comment
Watch Question

omegaomegaDeveloper
CERTIFIED EXPERT

Commented:
Hello, Ivan,

Can you make use of the Control.GetNextControl method?  It seems to do what you need.

Cheers,
Randy
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.