• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 454
  • Last Modified:

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?
0
lobo797
Asked:
lobo797
  • 5
  • 4
  • 2
1 Solution
 
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 TomlinsonMiddle School Assistant TeacherCommented:
(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
 
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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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 TomlinsonMiddle School Assistant TeacherCommented:
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 TomlinsonMiddle School Assistant TeacherCommented:
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 TomlinsonMiddle School Assistant TeacherCommented:
*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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 5
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now