Solved

How to tab to the next control when the enter key is pressed....  vb.net

Posted on 2006-11-06
7
270 Views
Last Modified: 2010-05-18
Im looking for an easy way to go to the next control in the tab index when the enter key is pressed.  Can you help me out???

vb.Net 2005

0
Comment
Question by:ExtremeFitness
7 Comments
 
LVL 17

Expert Comment

by:ZeonFlash
ID: 17884629
Try using SendKeys:

(This is VS2003 syntax, but I can't imagine that it has changed much for 2005)

      Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
            If keyData = Keys.Enter Then
                  SendKeys.Send("{TAB}")
            End If
      End Function
0
 
LVL 2

Author Comment

by:ExtremeFitness
ID: 17884678
Thats what I have right now, apparenlty the sendkey is bad though!!!!!!  BAD Sendkey!!!

So I was wondering if there is an alternative... :)

0
 
LVL 2

Author Comment

by:ExtremeFitness
ID: 17884689
I could set focus and all that, but that seems like Waaaaaaaayyyyyyyyyyyyyy to much work....

0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 13

Expert Comment

by:newyuppie
ID: 17884752
i personally prefer not to use SendKeys. the way i handle this is handle the KeyPress event of the textbox and use this code:

'test for the {enter} key
        If e.KeyChar = Convert.ToChar(Keys.Enter) Then
            'test for a Textbox control
            Dim txtbox As TextBox = TryCast(Me.ActiveControl, TextBox)
            If txtbox IsNot Nothing Then
                'Convert the {enter} key to a {tab}
                e.Handled = True
                Me.ProcessTabKey(True)
            Else
                'just pass it on
                e.Handled = False
                MyBase.OnKeyPress(e)
            End If
        End If


0
 
LVL 13

Expert Comment

by:newyuppie
ID: 17884795
 'test for a Textbox control
            Dim txtbox As TextBox = TryCast(Me.ActiveControl, TextBox)
            If txtbox IsNot Nothing Then

this part i included because in my code i am using a generalized sub so i can handle keypresses on textboxes, combos, etc. if you are just handling a single textbox you could skip those verifications. anyway this example shows how you could "just pass it on" meaning that you could just not handle the Enter as a Tab in a situation if you decide so.
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 17886620
Use the GetNextControl() method:

    Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
        If keyData = Keys.Enter Then
            Dim nextControl As Control = Me.GetNextControl(Me.ActiveControl, True)
            If Not (nextControl Is Nothing) Then
                nextControl.Focus()
            End If
        End If
    End Sub

See:
http://msdn2.microsoft.com/en-us/library/system.windows.forms.control.getnextcontrol(VS.80).aspx
0
 
LVL 7

Expert Comment

by:foobarr
ID: 17893748
Provided your Tab Order Is Correct

you could go

Private Sub EmployeeTextKeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtLogin.KeyPress, txtEMail.KeyPress, txtInitials.KeyPress, txtCell.KeyPress, txtPhone.KeyPress, txtCountry.KeyPress, txtPostal.KeyPress, txtCity.KeyPress, txtAddress.KeyPress, txtProvince.KeyPress, cboName.KeyPress


        If (e.KeyChar = Chr(Keys.Enter)) Then

            e.Handled = True

            Me.GetNextControl(Me.ActiveControl, True).Focus()



        End If

    End Sub
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question