Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everybody,

I am a little bit stuck on the following task:

I have a project with several Forms.
I need to get the "Enter" key from several forms but this does not work.
Only in the first form of the project it works properly.

What I have tried:

I use the following code to get in Enter in the first form:

Private Sub Start_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
  Select Case e.KeyCode
  Case Keys.Enter
 
  End Select

End Sub
Posted
Updated 2-Nov-17 5:49am
Comments
Richard MacCutchan 2-Nov-17 7:41am    
Key presses are sent to the Window (Form) that currently has the focus.
Member 9774614 2-Nov-17 9:39am    
But other keyboard inputs like normal characters e.g. a,b,c or 1,2,3 are sent
correctly on other forms. Only the enter does not work.
CHill60 2-Nov-17 10:04am    
No - key presses are sent to the window (form) that currently has the focus. How would you know Enter has not been sent with the code that you have posted - you're not even sending a message to the debug window (debug.print("enter pressed")).
Try putting a break point in there and examining the KeyCode that is sent.
Member 9774614 2-Nov-17 10:29am    
Sorry, but I forgot to comment the code above. It was only an incomplete snippet.
I tried to analyse the keyboarddata by writing it into the debug window.
Something like:
Private Sub Start_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
Debug.Print(CStr(e.KeyCode))
End Sub

It is working fine on form1. I get an output for normal characters and specialkeys like Enter. But when I open form2, it does not work. In form2 I get the output for 1,2,3 etc. but not for Enter.
Member 9774614 2-Nov-17 7:50am    
But other keyboard inputs like normal characters e.g. a,b,c or 1,2,3 are sent
correctly on other forms. Only the enter does not work.

1 solution

I found a solution:

In the load_event a usercontrol had to be selected.
In my case it was the textbox which had to be selected.
Private Sub form1_Load(sender As Object, e As EventArgs) Handles Me.Load
    textbox1.Select()
End Sub


Now everything is working fine.
 
Share this answer
 
Comments
Richard MacCutchan 2-Nov-17 12:50pm    
That is why my comment said "Key presses are sent to the Window (Form) that currently has the focus.". The key being the word "Window", as every control on a Form is a Window.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900