[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.
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:
Private Sub TextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
If e.KeyCode = Keys.Enter Then
Button1.PerformClick()
End If
End Sub
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:
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If Asc(e.KeyChar) = Keys.Enter Then
Button1.PerformClick()
End If
End Sub
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?
Re: [2005] How do you type in a text box while a button is focused?
vb 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
e.SuppressKeyPress = True
Button1.PerformClick()
End If
End Sub
Re: [2005] How do you type in a text box while a button is focused?
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.)