By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,552 Members | 893 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,552 IT Pros & Developers. It's quick & easy.

Execute Button Click event on Enter keypress

100+
P: 294
I am trying to make it easier on the end user. I have a button that populates a DataGridView, however instead of forcing the users to click the button every time, I would like the "Enter" keypress event to also handle what the button does.

Does anyone know how to do this? (Simply?)
Jan 13 '14 #1

✓ answered by Luk3r

If you're calling the function (Call EnterClick(sender, e)), but if you're just using If e.Keycode.Equals(Keys.Enter) Then, then you simply put the action within the If...Then statement. The reason I said it'd be best to call a function is because you could do it in this way:


Private Sub EnterClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs)
If e.KeyCode.Equals(Keys.Enter) Then
'code to populate your datagridview
End If
End Sub


Then, when you hit enter while the text box has focus, you simply call the event with Call EnterClick(sender, e).

Also, under your button click event, you would also use Call EnterClick(sender, e), since the button click and ENTER keypress would be completing the same task. This will save you from having duplicate code for two different events.

Here's some sample code that I think you could work from. Maybe just create a new app, add a button, textbox, and a datagridview and play with it:
Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.     Private Sub EnterClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs)
  3.         If e.KeyCode.Equals(Keys.Enter) Then
  4.             DataGridView1.Rows.Add(TextBox1.Text)
  5.         End If
  6.     End Sub
  7.  
  8.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  9.         Call EnterClick(sender, e)
  10.     End Sub
  11.  
  12.     Private Sub TextBox1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
  13.         Call EnterClick(sender, e)
  14.     End Sub
  15. End Class

Share this Question
Share on Google+
8 Replies


100+
P: 299
There are a couple of things you have to worry about here... the first being, what is actually selected on your form? The form itself? The button? A textbox? Whatever is "selected" is where you would want to detect the "ENTER" keypress. For example: Let's say I am wanting label1.text to equal what I type in textbox1.text, without a button. I would create a function and call it via keyup, keydown, or keypress of textbox1. It's hard to describe, so I will just give you an example:



Expand|Select|Wrap|Line Numbers
  1. 'Function that gets called when hitting "ENTER" inside textbox1
  2.  Private Sub EnterClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs)
  3.         If e.KeyCode.Equals(Keys.Enter) Then
  4.             label1.text = textbox1.text
  5.         End If
  6.     End Sub
  7.  
  8.  
  9. 'Checks for the "ENTER" key inside textbox1
  10. Private Sub TextBox1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
  11.         Call EnterClick(sender, e)
  12.     End Sub


Your other alternative would be to NOT use a function and simply check for the ENTER keyup, keydown, or keypress inside the textbox:

Expand|Select|Wrap|Line Numbers
  1. Private Sub TextBox1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
  2.         If e.KeyCode.Equals(Keys.Enter) Then
  3.             label1.text = textbox1.text
  4.         End If
  5.     End Sub
Jan 14 '14 #2

100+
P: 294
Great! Thanks. It is actually the OnEvent ButtonClick. I think I am trying to accomplish the latter of your examples. If Enter were pressed while the Last Name text box has focus, I would want to execute the code I have written for the OnClick Event for the Button.

I am not a great programmer, but how would I call the event in that scenario?

Private Sub SearchLastTxt_KeyDown(ByVal sender as System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles SearchLastTxt.KeyDown

If e.Keycode.Equals(Keys.Enter) Then
???? How do I call the button click event????
End If
End Sub
Jan 14 '14 #3

100+
P: 299
If you're calling the function (Call EnterClick(sender, e)), but if you're just using If e.Keycode.Equals(Keys.Enter) Then, then you simply put the action within the If...Then statement. The reason I said it'd be best to call a function is because you could do it in this way:


Private Sub EnterClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs)
If e.KeyCode.Equals(Keys.Enter) Then
'code to populate your datagridview
End If
End Sub


Then, when you hit enter while the text box has focus, you simply call the event with Call EnterClick(sender, e).

Also, under your button click event, you would also use Call EnterClick(sender, e), since the button click and ENTER keypress would be completing the same task. This will save you from having duplicate code for two different events.

Here's some sample code that I think you could work from. Maybe just create a new app, add a button, textbox, and a datagridview and play with it:
Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.     Private Sub EnterClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs)
  3.         If e.KeyCode.Equals(Keys.Enter) Then
  4.             DataGridView1.Rows.Add(TextBox1.Text)
  5.         End If
  6.     End Sub
  7.  
  8.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  9.         Call EnterClick(sender, e)
  10.     End Sub
  11.  
  12.     Private Sub TextBox1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
  13.         Call EnterClick(sender, e)
  14.     End Sub
  15. End Class
Jan 14 '14 #4

100+
P: 294
Thank you for the response. Great answer, by the way.
Jan 14 '14 #5

100+
P: 294
One more question, for the row:

Expand|Select|Wrap|Line Numbers
  1.  DataGridView1.Rows.Add(TextBox1.Text)
I am using this to populate the datagridview1. How would I change it to accomodate this function?

Expand|Select|Wrap|Line Numbers
  1.  If SearchFirsttxt.Text = "" Then
  2.             SqlCommand.CommandText = "Select * FROM PARTICIPANT WHERE LAST_NM_TXT = '" & eLast & "';"
  3.         ElseIf SearchLastTxt.Text = "" Then
  4.             SqlCommand.CommandText = "Select * FROM PARTICIPANT WHERE FIRST_NM_TXT = '" & eFirst & "';"
  5.         Else
  6.             SqlCommand.CommandText = "Select * FROM PARTICIPANT WHERE FIRST_NM_TXT = '" & eFirst & "' and LAST_NM_TXT = '" & eLast & "';"
  7.         End If
Expand|Select|Wrap|Line Numbers
  1. Dim myAdapter As New SqlDataAdapter(SqlCommand) 'holds the data
  2.         myAdapter.Fill(dt) 'datatable that is populated into the holder (DataAdapter)
  3.         DataGridView1.DataSource = dt 'Assigns source of information to the gridview (DataTable)
Jan 14 '14 #6

100+
P: 299
Simply replace DataGridView1.Rows.Add(TextBox1.Text) with your code that populates the data. :)
Jan 14 '14 #7

P: 2
Expand|Select|Wrap|Line Numbers
  1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  2.      'code for populating datagridview
  3. End Sub
  4.  
  5. Private Sub Button1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Button1.KeyPress
  6.  
  7.      If Asc(e.KeyChar) = 13 Then
  8.            Button1_Click(Me, EventArgs.Empty)
  9.      End if
  10.  
  11. End Sub
Jan 30 '14 #8

100+
P: 299
@justkidding, mcupito stated that he would have focus on a textbox that when hitting ENTER it would perform the same action as the button. So using a Button Keypress event would not suffice. That being said, if you changed your Button Keypress to a Textbox Keypress, your code is a great alternative (for those that don't like using sub methods).
Jan 30 '14 #9

Post your reply

Sign in to post your reply or Sign up for a free account.