Results 1 to 12 of 12

Thread: [RESOLVED] [2008] Quick question about pressing enter event

  1. #1

    Thread Starter
    Member Daarklord's Avatar
    Join Date
    Jul 2008
    Posts
    54

    Resolved [RESOLVED] [2008] Quick question about pressing enter event

    If a user presses enter in a textbox, i want to activate a button that's next to it. What's the easiest way to do this?
    0xABADA55D00D

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] Quick question about pressing enter event

    Handle the KeyDown event of the TextBox and call the PerformClick event of the Button.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [2008] Quick question about pressing enter event

    Or assign that button to your form's AcceptButton property.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  4. #4
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,514

    Re: [2008] Quick question about pressing enter event

    Check to see if "Enter" was pressed, then do whatever you want.

    vb Code:
    1. Private Sub Text1_KeyPress(KeyAscii As Integer)
    2. If KeyAscii = vbKeyReturn Then
    3.     MsgBox "Enter"
    4.     Button1.SetFocus
    5. Else
    6.     MsgBox "Not Enter"
    7. End If
    8. End Sub

  5. #5
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [2008] Quick question about pressing enter event

    Quote Originally Posted by wes4dbt
    Check to see if "Enter" was pressed, then do whatever you want.

    vb Code:
    1. Private Sub Text1_KeyPress(KeyAscii As Integer)
    2. If KeyAscii = vbKeyReturn Then
    3.     MsgBox "Enter"
    4.     Button1.SetFocus
    5. Else
    6.     MsgBox "Not Enter"
    7. End If
    8. End Sub
    That's VB6 code and it will not work on VB.Net application.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  6. #6

    Thread Starter
    Member Daarklord's Avatar
    Join Date
    Jul 2008
    Posts
    54

    Re: [2008] Quick question about pressing enter event

    Thanks all. Well this is my solution

    Code:
    Private Sub TextBoxUsersName_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBoxUsersName.KeyPress
            If e.KeyChar = Microsoft.VisualBasic.ChrW(Windows.Forms.Keys.Return) Then
                ButtonUsersName_Click(TextBoxUsersName, e)
            End If
        End Sub
    But I was hoping that such a common function would be supported as something that's precoded as an option, like the way you can set a combobox to suggest completions. AcceptButton is a good idea, but I have many textboxes and buttons on the same form. If I suggest Microsoft to add this functionality to the next VS would they do it?

    Thanks all.
    Last edited by Daarklord; Jul 18th, 2008 at 02:21 PM.
    0xABADA55D00D

  7. #7
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [2008] Quick question about pressing enter event

    You should replace this line
    Code:
    ButtonUsersName_Click(TextBoxUsersName, e)
    With this
    Code:
    ButtonUsersName.PerformClick()
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  8. #8

    Thread Starter
    Member Daarklord's Avatar
    Join Date
    Jul 2008
    Posts
    54

    Re: [2008] Quick question about pressing enter event

    Any reason why PerformClick is better?
    0xABADA55D00D

  9. #9
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,514

    Re: [2008] Quick question about pressing enter event

    Sorry I thought I was in the VB6 forum.
    I guess I'll have to get my eyes checked.

  10. #10

    Thread Starter
    Member Daarklord's Avatar
    Join Date
    Jul 2008
    Posts
    54

    Re: [2008] Quick question about pressing enter event

    Quote Originally Posted by wes4dbt
    Sorry I thought I was in the VB6 forum.
    I guess I'll have to get my eyes checked.
    Haha no worries. I recognized the VB6-ishness of it .
    0xABADA55D00D

  11. #11
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [2008] Quick question about pressing enter event

    Quote Originally Posted by Daarklord
    Any reason why PerformClick is better?
    It's the proper way to trigger a button click in code. While calling the method directly and suppy it with parameters (as you're doing) does work, it's generally considered to be bad practice.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  12. #12

    Thread Starter
    Member Daarklord's Avatar
    Join Date
    Jul 2008
    Posts
    54

    Re: [2008] Quick question about pressing enter event

    And that concludes my quick question about pressing enter.
    0xABADA55D00D

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width