Question

I have a textbox called clientFirst and, if I press Enter after I'm done typing in this textbox, I want the Enter button to represent a click on btnAdd. How can I make this happen?

Protected Sub btnAdd_Click(sender As Object, e As ImageClickEventArgs) Handles btnAdd.Click
    'Do something       
End Sub

There's not much code to post, really. :( But I was suggested to use the KeyDown function by Chase Ernst, which seems like a brilliant idea, except that it keeps telling me that a part of it is undefined. Here's the code I was suggested:

Private Sub clientFirst_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles clientFirst.KeyDown
    If e.KeyCode = Keys.Enter Then
        btnAdd.PerformClick()
    End If
End Sub

It keeps telling me that System.Windows.Forms.KeyEventArgs is undefined. I'm guessing there's an Imports class I have to define at the top? Tried System.Windows, System.Object, System.Windows.Forms, System.KeyEventArgs and nothing works!

Thank you in advance! x

Was it helpful?

Solution

Easy way is to use the HTMLForm.DefaltButton property on the form.

 Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
     Page.Form.DefaultButton = "TextBox1"
 End Sub

OTHER TIPS

I would use something like this:

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

If the key that is pressed is the enter key, then Button1.PerformClick() will fire.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top