Execute Button Click event on Enter keypress

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mcupito
    Contributor
    • Aug 2013
    • 294

    Execute Button Click event on Enter keypress

    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?)
  • Luk3r
    Contributor
    • Jan 2014
    • 300

    #2
    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:



    Code:
    'Function that gets called when hitting "ENTER" inside textbox1
     Private Sub EnterClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs)
            If e.KeyCode.Equals(Keys.Enter) Then
                label1.text = textbox1.text
            End If
        End Sub
    
    
    'Checks for the "ENTER" key inside textbox1
    Private Sub TextBox1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
            Call EnterClick(sender, e)
        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:

    Code:
    Private Sub TextBox1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
            If e.KeyCode.Equals(Keys.Enter) Then
                label1.text = textbox1.text
            End If
        End Sub
    Last edited by Luk3r; Jan 14 '14, 06:06 PM. Reason: Forgot to use code tags

    Comment

    • mcupito
      Contributor
      • Aug 2013
      • 294

      #3
      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_K eyDown(ByVal sender as System.Object, ByVal e As System.Windows. Forms.KeyEventA rgs) Handles SearchLastTxt.K eyDown

      If e.Keycode.Equal s(Keys.Enter) Then
      ???? How do I call the button click event????
      End If
      End Sub

      Comment

      • Luk3r
        Contributor
        • Jan 2014
        • 300

        #4
        If you're calling the function (Call EnterClick(send er, e)), but if you're just using If e.Keycode.Equal s(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(ByVa l sender As System.Object, ByVal e As System.Windows. Forms.KeyEventA rgs)
        If e.KeyCode.Equal s(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(send er, e).

        Also, under your button click event, you would also use Call EnterClick(send er, 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:
        Code:
        Public Class Form1
            Private Sub EnterClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs)
                If e.KeyCode.Equals(Keys.Enter) Then
                    DataGridView1.Rows.Add(TextBox1.Text)
                End If
            End Sub
        
            Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
                Call EnterClick(sender, e)
            End Sub
        
            Private Sub TextBox1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
                Call EnterClick(sender, e)
            End Sub
        End Class

        Comment

        • mcupito
          Contributor
          • Aug 2013
          • 294

          #5
          Thank you for the response. Great answer, by the way.

          Comment

          • mcupito
            Contributor
            • Aug 2013
            • 294

            #6
            One more question, for the row:

            Code:
             DataGridView1.Rows.Add(TextBox1.Text)
            I am using this to populate the datagridview1. How would I change it to accomodate this function?

            Code:
             If SearchFirsttxt.Text = "" Then
                        SqlCommand.CommandText = "Select * FROM PARTICIPANT WHERE LAST_NM_TXT = '" & eLast & "';"
                    ElseIf SearchLastTxt.Text = "" Then
                        SqlCommand.CommandText = "Select * FROM PARTICIPANT WHERE FIRST_NM_TXT = '" & eFirst & "';"
                    Else
                        SqlCommand.CommandText = "Select * FROM PARTICIPANT WHERE FIRST_NM_TXT = '" & eFirst & "' and LAST_NM_TXT = '" & eLast & "';"
                    End If
            Code:
            Dim myAdapter As New SqlDataAdapter(SqlCommand) 'holds the data
                    myAdapter.Fill(dt) 'datatable that is populated into the holder (DataAdapter)
                    DataGridView1.DataSource = dt 'Assigns source of information to the gridview (DataTable)

            Comment

            • Luk3r
              Contributor
              • Jan 2014
              • 300

              #7
              Simply replace DataGridView1.R ows.Add(TextBox 1.Text) with your code that populates the data. :)

              Comment

              • justkidding
                New Member
                • Jan 2014
                • 2

                #8
                Code:
                Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
                     'code for populating datagridview
                End Sub
                
                Private Sub Button1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Button1.KeyPress
                
                     If Asc(e.KeyChar) = 13 Then
                           Button1_Click(Me, EventArgs.Empty)
                     End if
                
                End Sub

                Comment

                • Luk3r
                  Contributor
                  • Jan 2014
                  • 300

                  #9
                  @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).

                  Comment

                  Working...