Tabbing between fields in VB Form

I am building a form for a user interface.  I have multiple textboxes for entry that I can tab through in the order I prefer.  My question is how to go through the same textboxes in order using the 'Enter' key.  Can this be done with setting properties, or does it have to be done via code?
LVL 10
lobo797Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

danaseamanCommented:
Try this:

Option Explicit

Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
   If KeyCode = 13 Then
      SendKeys "{TAB}"
   End If
End Sub

Private Sub Text2_KeyDown(KeyCode As Integer, Shift As Integer)
   If KeyCode = 13 Then
      SendKeys "{TAB}"
   End If
End Sub

Private Sub Text3_KeyDown(KeyCode As Integer, Shift As Integer)
   If KeyCode = 13 Then
      SendKeys "{TAB}"
   End If
End Sub

Open in new window

0
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
(posted in the Visual Studio.Net zone)

How about?
Public Class Form1

    Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
        Select Case keyData
            Case Keys.Enter
                Debug.WriteLine("Enter")
                Dim ctl As Control = Me.ActiveControl
                If Not IsNothing(ctl) AndAlso TypeOf ctl Is TextBox Then
                    SendKeys.Send("{TAB}")
                    Return True ' suppress default behaviour
                End If

        End Select
        Return MyBase.ProcessCmdKey(msg, keyData)
    End Function

End Class

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
lobo797Author Commented:
I failed to mention that I'm a novice with code.

danaseaman, I edited the fields to match the ones that I wanted to trigger as shown below.  The code runs fine, but nothing triggers the code.  I'm missing something.

Idle_Mind, when I attempt to use the code you sent, I get a 'SecurityException was Unhandled' error which I can't get past.
Option Explicit On
Imports System.Windows.Forms
Public Class Cheatsheet

    Private Sub TxtCost1_KeyDown(ByVal KeyCode As Integer, ByVal Shift As Integer)
        If KeyCode = 13 Then
            MsgBox("Hi")
            SendKeys.Send("{TAB}")
        End If

    End Sub

    Private Sub TxtLength1_KeyDown(ByVal KeyCode As Integer, ByVal Shift As Integer)
        If KeyCode = 13 Then
            SendKeys.Send("{TAB}")
        End If
    End Sub

    Private Sub TxtCost2_KeyDown(ByVal KeyCode As Integer, ByVal Shift As Integer)
        If KeyCode = 13 Then
            SendKeys.Send("{TAB}")
        End If
    End Sub

Open in new window

0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
Not sure why you're getting that exception...  =\

Try it with blank Form and a couple of TextBoxes.

Here is how it would look inside your form code:
Option Explicit On
Imports System.Windows.Forms
Public Class Cheatsheet

    Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
        Select Case keyData
            Case Keys.Enter
                Dim ctl As Control = Me.ActiveControl
                If Not IsNothing(ctl) AndAlso TypeOf ctl Is TextBox Then
                    SendKeys.Send("{TAB}")
                    Return True ' suppress default behaviour
                End If

        End Select
        Return MyBase.ProcessCmdKey(msg, keyData)
    End Function

End Class

Open in new window

0
lobo797Author Commented:
I started a new project, put a couple textboxes on and pasted the code above.  I still get the error message attached.  I tried the suggestion action of adding permissions to the project with no success.

Thanks for the help so far.  I will be out until 7:00am CST tomorrow.


Error.jpg
0
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
Hmm...

What version Visual Studio are you using?

What kind or Project did you create?

Does a Project with just a Plain Form with nothing on it and no Code generate any errors?
0
lobo797Author Commented:
Idle_Mind,

I'm using Visual Studio.Net 2008 v9.0

I created a quick form on my install at home and it works perfect.  There must be something in wrong with my install at work.  I just set it up a couple weeks ago.

I would like to give danaseaman a little time to see if there is any input on why that code doesn't work.  I tested it here as well with no success.

Thanks, again.
0
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
The VB.Net equivalent of danaseaman's approach would look like:
Public Class Form1

    Private Sub TB_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress, TextBox2.KeyPress, TextBox3.KeyPress
        If e.KeyChar = Microsoft.VisualBasic.ChrW(13) Then
            e.Handled = True
            SendKeys.Send("{TAB}")
        End If
    End Sub

End Class

Open in new window

0
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
*Note the multiple controls list after the "Handles" keyword in line #3.
0
danaseamanCommented:
Sorry, my solution was for Vb6.
Go with IdleMinds'solution.
0
lobo797Author Commented:
Thanks for all the input!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.