[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Focus at Next Control when Enter Key is Pressed.

Posted on 2008-01-31
12
Medium Priority
?
930 Views
Last Modified: 2008-03-06
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

0
Comment
Question by:emi_sastra
12 Comments
 
LVL 23

Expert Comment

by:Ashish Patel
ID: 20787747
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
 
LVL 4

Expert Comment

by:RamanaChoudary
ID: 20787773
hi
you can use this code too

  SendKeys.SendWait("{ENTER}")
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 20787774
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
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
LVL 1

Author Comment

by:emi_sastra
ID: 20787816
Hi RamanaChoudary,

Stack error, where to code it?

Thank you.
0
 
LVL 1

Expert Comment

by:DodgyRef
ID: 20787843
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
 
LVL 23

Expert Comment

by:Ashish Patel
ID: 20787862
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
 
LVL 1

Author Comment

by:emi_sastra
ID: 20787885
Hi DodgyRef

SelectNextControl is not declared.

Thank you.
0
 
LVL 23

Expert Comment

by:Ashish Patel
ID: 20787929
Did you try the last solution i gave with minor fix?
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 20787945
Hi Asvforce,

It doesn't move to next control.

Thank you.
0
 
LVL 23

Accepted Solution

by:
Ashish Patel earned 2000 total points
ID: 20788025
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
 
LVL 1

Author Comment

by:emi_sastra
ID: 20788089
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
 
LVL 1

Author Comment

by:emi_sastra
ID: 20788118
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

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

Question has a verified solution.

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

Article by: Jorge
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…
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
How to fix display issue, screen flickering issue when I plug in power cord to the machine. Before I start explaining the solution lets check out once the issue how it looks like after I connect the power cord. most of you also have faced this…
This video tutorial shows you the steps to go through to set up what I believe to be the best email app on the android platform to read Exchange mail.  Get the app on your phone: The first step is to make sure you have the Samsung Email app on your …
Suggested Courses

607 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