Results 1 to 8 of 8

Thread: [RESOLVED] Assiging Enter key to multiple buttons

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2019
    Posts
    16

    Resolved [RESOLVED] Assiging Enter key to multiple buttons

    Hey. I need to press the enter key for different textboxes and their related buttons.

    Name:  ss.png
Views: 449
Size:  2.2 KB

    These textboxes are not related to each other. I often use this statement if there is only one button:

    Name:  ss2.jpg
Views: 527
Size:  6.8 KB

    How can I do this?

  2. #2
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,834

    Re: Assiging Enter key to multiple buttons

    Please remember next time...elections matter!

  3. #3
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    845

    Re: Assiging Enter key to multiple buttons

    you may try something like :

    VB.net Code:
    1. Private sub enter_keydown (ByVal sender as object, ByVal e as System.Windows.Form.KeyEventArgs) handles textbox1.Keydown, textbox2.keydown, ....
    2. dim name=ctype(sender,textbox).name
    3.  
    4. If e.KeyCode = Keys.Enter Then
    5.      
    6.      Select case name
    7.  
    8.        case "Textbox1"
    9.      button1_Click(sender,e)
    10.         .
    11.         .
    12.       Case "Textbox4"
    13.         button4_Click(sender,e)
    14.  
    15.     end select
    16.  
    17. end if
    18. end sub
    Last edited by Delaney; Nov 23rd, 2020 at 10:21 AM.
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
    “They did not know it was impossible so they did it” (Mark Twain)

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Assiging Enter key to multiple buttons

    Quote Originally Posted by Delaney View Post
    you may try something like :

    Code:
    Private sub enter_keydown (ByVal sender as object, ByVal e as System.Windows.Form.KeyEventArgs) handles textbox1.Keydown, textbox2.keydown, ....
    dim name=ctype(sender,textbox).name
    
    If e.KeyCode = Keys.Enter Then
         
         Select case name
    
           case "Textbox1"
               button1_Click(sender,e)
            .
            .
          Case "Textbox4"
              button4_Click(sender,e)
    
        end select
    
    end if
    end sub
    Code:
    button1.PerformClick
    or

    Code:
    button4.PerformClick

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jul 2019
    Posts
    16

    Re: Assiging Enter key to multiple buttons

    TysonLPrice, Delaney and paul. Many thanks. Much appreciated.
    Last edited by teos983; Nov 24th, 2020 at 03:13 PM.

  6. #6
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,834

    Re: Assiging Enter key to multiple buttons

    Quote Originally Posted by teos983 View Post
    Delaney and paul. Many thanks. Much appreciated.
    I guess I should have posted code for you instead of a link on how to do it.
    Please remember next time...elections matter!

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Jul 2019
    Posts
    16

    Re: Assiging Enter key to multiple buttons

    Quote Originally Posted by TysonLPrice View Post
    I guess I should have posted code for you instead of a link on how to do it.
    No. This is not true. Your link was very helpful. You won't believe me I know. But I forgot to include your name when writing a "thank you" reply. I hope you forgive me for this mistake.

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: [RESOLVED] Assiging Enter key to multiple buttons

    Looking at Delaney's code, you could use something like this...

    Code:
    Public Class Form5
    
        Dim pairs As New Dictionary(Of TextBox, Button)
    
        Private Sub Form5_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            pairs.Add(TextBox1, Button6)
            pairs.Add(TextBox2, Button7)
            pairs.Add(TextBox3, Button8)
            pairs.Add(TextBox4, Button9)
    
            For Each kvp As KeyValuePair(Of TextBox, Button) In pairs
                AddHandler kvp.Key.KeyDown, AddressOf enter_keydown
                AddHandler kvp.Value.Click, AddressOf buttons_click
            Next
        End Sub
    
        Private Sub enter_keydown(ByVal sender As Object, ByVal e As KeyEventArgs)
            If e.KeyCode = Keys.Enter Then
                pairs(DirectCast(sender, TextBox)).PerformClick()
            End If
        End Sub
    
        Private Sub buttons_click(ByVal sender As Object, ByVal e As EventArgs)
            MsgBox(DirectCast(sender, Button).Text)
        End Sub
        
    End Class

Tags for this Thread

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