locked
Textbox control and the Keyboard's Enter Key RRS feed

  • Question

  • How do I handle the event of pressing the  keyboard's Enter Key in a VS2008 Visual Basic application? 
    I have a Textbox control (Textbox1) and a label control (Label1) as the only items on my form, I want the Text in textbox1 to be copied to label1 when I press the keyboard's Enter Key . I used the following code:

    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress

            Label1.Text = TextBox1.Text

    End Sub

    I did't get the desired result as the label control is updated after any key press. No default method for EnterKeyPress?
    I want to finish typing a word then press the Enter key before the label control is updated. . . How can I write such a method?

    Anticipating your kind replies.
     

     


    Laborare est Orare...
     
    Monday, September 21, 2009 10:31 PM

Answers

  • here is one way to do it


    Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) _
        Handles TextBox1.KeyDown
            
            If e.KeyValue = Keys.Enter Then
                Label1.Text = TextBox1.Text
            End If
        End Sub
    • Marked as answer by Glogo Wednesday, September 23, 2009 9:59 PM
    Monday, September 21, 2009 11:25 PM

All replies

  • here is one way to do it


    Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) _
        Handles TextBox1.KeyDown
            
            If e.KeyValue = Keys.Enter Then
                Label1.Text = TextBox1.Text
            End If
        End Sub
    • Marked as answer by Glogo Wednesday, September 23, 2009 9:59 PM
    Monday, September 21, 2009 11:25 PM
  • Hi Glogo,

    Use the following code:-

    Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
            If e.KeyCode = Keys.Enter Then
                Label1.Text = TextBox1.Text
            End If
    End Sub

    Regards,
    Manoj Mishra


    • Proposed as answer by manoj_mishra_18 Monday, September 21, 2009 11:34 PM
    Monday, September 21, 2009 11:33 PM
  • The code from jwavila is definitely what you were looking for.  I just wanted to add that you can also handle the Leave event of the textbox so that in case the user forgets to press Enter the text will still be copied when the user brings the focus to another control.  I realize that this may not be how your design is supposed to behave, but since you did not explicily say otherwise I thought I'd mention it in case it was useful.
    Tuesday, September 22, 2009 12:51 AM
  • Thanks Dig-Boy,
    I appreciate your contribution. I will try them and get back to Forums with my reports.
    Laborare est Orare...
    Wednesday, September 23, 2009 9:56 PM
  • Thanks a bunch jwavila, this answer has really solved my programming problem, I am preparing the reports I promised(23rd Sept. 2009)  to deliver to Forums on this issue.
     cheers. . . :)
    Laborare est Orare...
    Tuesday, September 29, 2009 9:48 PM