Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2006-11-06
7
Medium Priority
?
275 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 86

Accepted Solution

by:
Mike Tomlinson earned 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

721 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