Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I know this question clearly defines me ass the dolt that I am.

However, I have looked in MS Help Files, Dot Net Heaven, and the Related Entry "Application Event Handler for WinForms" here. Dot Net Heaven had nothing, and MS Help, and "Application Event Handler for WinForms" were somewhat incomprehensible to me.

I have a VB.net desktop form, that has a text box on it. I want my users to be able to type into it and then press the Enter Key, which would be trapped, and the cursor would move to the next text box. If you can give me a simple explanation, or tell me what I am doing wrong in the code below I would appreciate it.

What I have tried:

Public Class UserLogin
    Dim WithEvents Press As TextBoxBase
    Private Sub CmdOK_Click(sender As Object, e As EventArgs) Handles cmdOK.Click
        On Error GoTo OT_Handle_Error
        Dim sProcedureName As String = "CmdOK_Click"

        Title.Show()
        Close()

        Exit Sub
OT_Handle_Error:
        ReportFrom("OpenCompany", sProcedureName)
    End Sub

    Private Sub CmdCancel_Click(sender As Object, e As EventArgs) Handles cmdCancel.Click
        On Error GoTo OT_Handle_Error
        Dim sProcedureName As String = "CmdCancel_Click"

        Title.Show()
        Close()

        Exit Sub
OT_Handle_Error:
        ReportFrom("OpenCompany", sProcedureName)


    End Sub

    Private Sub txtUserName_KeyPressed(sender As Object, e As EventArgs) Handles txtUserName.Press

    End Sub
End Class
Posted
Updated 19-Apr-22 1:37am

VB
Private Sub txtUserName_KeyPressed(sender As Object, e As EventArgs) Handles txtUserName.Press

As far as I am aware there is no TextBox event named Press. The event you want is named KeyPress
 
Share this answer
 
Comments
Slow Eddie 19-Apr-22 7:48am    
Thank you for your reply. However, I have already tried the KeyPress event. It fires on every single character. I guess really need to know how to trap the Enter Key, and ignore the others.
Richard MacCutchan 19-Apr-22 8:02am    
Looking at the documentation, it would appear that the KeyPress event does not fire when you press the Enter key (the documentation only implies this). You may need to use the Control.KeyDown Event (System.Windows.Forms) | Microsoft Docs[^].
The answer from Richard is not complete correct ...
With the KeyPressed-Event from a Control (Textbox) you get not e as EventArgs but e as KeyPressEventArgs.
The Difference is that with this Arguments you get a class which delivers you with e.KeyChar the Code of the pressed Key. Each Key which is pressed inside this Controls fires this Event - but now you could check for the Enter-Key and do the reaction you like to have - for example give the Focus to another Control ...
 
Share this answer
 
Comments
Slow Eddie 19-Apr-22 7:45am    
Yes that is the problem I was running into. I will check the e as KeyPressEventArgs. Thank you for that clarification. Ed
Ralf Meier 19-Apr-22 8:38am    
Hi Eddie,
I have to say that I'm not quiet sure at the moment if the KeyPressed-Event is exactly the right approach (in the moment I'm not able to look for it). If it is right you will get the Enter-Key as possible KeyCode-selection. Otherwise you should look for what Richard has mentioned - the KeyDown-Event. Also there you have EventArgs which gives you much more Info than the "Standard"-EventArgs - but you are allways able to select that because all Special-EventArgs are deriving from this class ...

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



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