Solved

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

Posted on 2006-11-06
7
268 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

707 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now