Solved

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

Posted on 2006-11-06
7
271 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ASP/VB email question 4 46
VB.Net - MemoryMappedFiles - Confirm receipt 2 24
Optimize the query 5 43
Code enhancement 4 21
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…

861 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