Results 1 to 7 of 7

Thread: [RESOLVED] [2005] How do you type in a text box while a button is focused?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    219

    Resolved [RESOLVED] [2005] How do you type in a text box while a button is focused?

    I'm making an app and when the user types in a textbox and presses enter-enter acts as the button that does something to the text.
    Could somebody please help me?
    Like on Google when you type in something and press enter and it returns a page of results.
    Last edited by Louix; Dec 19th, 2007 at 01:04 PM.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: [2005] How do you type in a text box while a button is focused?

    you can catch the enter key in the TextBox1_KeyUp event

    vb Code:
    1. Private Sub TextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
    2.         If e.KeyCode = Keys.Enter Then
    3.             Button1.PerformClick()
    4.         End If
    5. End Sub

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: [2005] How do you type in a text box while a button is focused?

    It probably doesn't matter much, but generally I use the KeyPress event
    vb.net Code:
    1. Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
    2. If Asc(e.KeyChar) = Keys.Enter Then  
    3.    Button1.PerformClick()
    4. End If
    5. End Sub

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    219

    Re: [2005] How do you type in a text box while a button is focused?

    Thanks alot guys but is there anyway to stop it making the noise when you press enter?
    Last edited by Louix; Dec 19th, 2007 at 01:27 PM.

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: [2005] How do you type in a text box while a button is focused?

    vb Code:
    1. Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
    2.      If e.KeyCode = Keys.Enter Then
    3.          e.SuppressKeyPress = True
    4.          Button1.PerformClick()
    5.      End If
    6. End Sub

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    219

    Re: [2005] How do you type in a text box while a button is focused?

    Thank you so so much!

  7. #7
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657

    Re: [RESOLVED] [2005] How do you type in a text box while a button is focused?

    Another option that seems to have been overlooked here: You can set the AcceptButton property of the form. The button you choose will fire its Click event when you press Enter. (In VB6 and before, this is the equivalent of setting a button's "Default" property to True.)
    "It's cold gin time again ..."

    Check out my website here.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width