Focus at Next Control when Enter Key is Pressed.

HI,

I need to focus to next control when Enter Key is pressed.

The keypreview =true

I try below code, but it doesn't work.

What's wrong with my code?

Thank you.
Private Sub Form_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
        ERV_Global.Focus_To_Next_Control(Me, sender, e.KeyChar)
    End Sub
 
 
  Public Shared Sub Focus_To_Next_Control(ByVal frmForm As Form, ByVal sender As Object, ByVal strKeyCode As String)
 
        If strKeyCode = ChrW(Keys.Enter) Then
            Dim oNextControl As System.Windows.Forms.Control = frmForm.GetNextControl(sender, True)
            oNextControl.Focus()
        End If
 
    End Sub

Open in new window

LVL 1
emi_sastraAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ashish PatelCommented:
Try this
Private Sub Form_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
        ERV_Global.Focus_To_Next_Control(Me, sender, e)
    End Sub
 
 
  Public Shared Sub Focus_To_Next_Control(ByVal frmForm As Form, ByVal sender As Object, ByVal strKeyCode As System.Windows.Forms.KeyPressEventArgs)
 
        If strKeyCode = Microsoft.VisualBasic.ChrW(13)  Then
            Dim oNextControl As System.Windows.Forms.Control = frmForm.GetNextControl(sender, True)
            oNextControl.Focus()
        End If
 
    End Sub

Open in new window

0
RamanaChoudaryCommented:
hi
you can use this code too

  SendKeys.SendWait("{ENTER}")
0
emi_sastraAuthor Commented:
Hi Asvforce.

It has error:

Error      1      Operator '=' is not defined for types 'System.Windows.Forms.KeyPressEventArgs' and 'Char'.      

 If strKeyCode = Microsoft.VisualBasic.ChrW(13) Then

Thank you.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

emi_sastraAuthor Commented:
Hi RamanaChoudary,

Stack error, where to code it?

Thank you.
0
DodgyRefCommented:
Use the SelectNextControl method and the ActiveControl

Private Sub Form_KeyPress(ByVal sender As Object, ByVal e As  System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
        If e.KeyChar = ChrW(Keys.Enter) Then
            SelectNextControl(sender.ActiveControl, True, True, True, True)
        End If
    End Sub
0
Ashish PatelCommented:
oops, please use this. minor mistake

Private Sub Form_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
        ERV_Global.Focus_To_Next_Control(Me, sender, e)
    End Sub
 
 
  Public Shared Sub Focus_To_Next_Control(ByVal frmForm As Form, ByVal sender As Object, ByVal strKeyCode As System.Windows.Forms.KeyPressEventArgs)
 
        If strKeyCode.KeyChar = Microsoft.VisualBasic.ChrW(13)  Then
            Dim oNextControl As System.Windows.Forms.Control = frmForm.GetNextControl(sender, True)
            oNextControl.Focus()
        End If
 
    End Sub

Open in new window

0
emi_sastraAuthor Commented:
Hi DodgyRef

SelectNextControl is not declared.

Thank you.
0
Ashish PatelCommented:
Did you try the last solution i gave with minor fix?
0
emi_sastraAuthor Commented:
Hi Asvforce,

It doesn't move to next control.

Thank you.
0
Ashish PatelCommented:
Okay here you go, should work
Private Sub Form_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
        ERV_Global.Focus_To_Next_Control(sender, e)
    End Sub
 
 
  Public Shared Sub Focus_To_Next_Control(ByVal sender As Object, ByVal strKeyCode As System.Windows.Forms.KeyPressEventArgs)
 
        If strKeyCode.KeyChar = Microsoft.VisualBasic.ChrW(13)  Then
            Me.GetNextControl(Me.ActiveControl, False).Focus()
        End If
 
    End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
emi_sastraAuthor Commented:
Hi Asvforce,

Your code :
 Me.GetNextControl(Me.ActiveControl, False).Focus()

 should change to :

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

It works.

You solve my problem.

Thank you very much.


0
emi_sastraAuthor Commented:
Hi Asvforce,

Also :

 Public Shared Sub Focus_To_Next_Control(ByVal sender As Object, ByVal strKeyCode As System.Windows.Forms.KeyPressEventArgs)

should changed to:

 Public Shared Sub Focus_To_Next_Control(ByVal frmForm As Form, ByVal sender As Object, ByVal strKeyCode As System.Windows.Forms.KeyPressEventArgs)

Below are the completed code.

Thank you.
 

 Public Shared Sub Focus_To_Next_Control(ByVal frmForm As Form, ByVal sender As Object, ByVal strKeyCode As System.Windows.Forms.KeyPressEventArgs)
 
        If strKeyCode.KeyChar = Microsoft.VisualBasic.ChrW(13) Then
            frmForm.GetNextControl(frmForm.ActiveControl, True).Focus()
        End If
 
    End Sub

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.