Solved

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

Posted on 2006-11-06
7
269 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

863 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

20 Experts available now in Live!

Get 1:1 Help Now