473,322 Members | 1,671 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Execute Button Click event on Enter keypress

294 256MB
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

8 71496
Luk3r
300 256MB
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
mcupito
294 256MB
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
Luk3r
300 256MB
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
mcupito
294 256MB
Thank you for the response. Great answer, by the way.
Jan 14 '14 #5
mcupito
294 256MB
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
Luk3r
300 256MB
Simply replace DataGridView1.Rows.Add(TextBox1.Text) with your code that populates the data. :)
Jan 14 '14 #7
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
Luk3r
300 256MB
@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

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

Similar topics

3
by: Cary Linkfield | last post by:
I have a standard form I use. It inherits from Windows.Forms.Form. I usually add a Cancel button the form in the designer. I want to raise the Cancel button's Click event when the user presses...
6
by: Michael Johnson Jr. | last post by:
I am trying to handle a button click event, which updates a web control table with data. The button is dynamically created in the table itself. When I call updateTable() in the Page_Load the new...
1
by: Timothy V | last post by:
Hi, How do i make an asp:Button execute its Click event when the Enter key is pressed? Thanks in advance, Tim.
20
by: anthonymelillo | last post by:
Is there a way to call a button click event from somewhere else ? I have a text box where I would like to call a button click event when a user presses enter in the text box. Can I do this ?...
3
by: Robert W. | last post by:
I'm new to ASP.net programming so excuse my ignorance if the following question seems overly simplistic. I've created a simple Login form with 3 primary WebControls: - A TextBox for the Username...
17
by: Eric | last post by:
I'm new to JavaScript and I wrote this code to play with. Oddly, if I enter text in a box and then press the button, I only get the onChange event for the text box and not the button's onclick...
1
by: Joey | last post by:
I have an asp.net 1.1/C# web form with a text box server control and one button server control. Users can type in text to search and then click the button to start the search. I want them to be...
0
by: nraji | last post by:
hi, I have designed a web part using VS 2003 (Web control library template). I need to publish that in share point. Its not like drag and drop the things from toolbar. Every thing I need to do in...
5
by: S_K | last post by:
This is a weird problem.. I have a page with a textbox and a button (along with a bunch of other stuff). Both the textbox and the button have their SEPERATE events defined for the textchanged...
1
by: RyanT | last post by:
Access 2000 Windows XP I have a small form with two text boxes: - User ID - Password The form also has two command buttons: - Login - Cancel
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.