Hey. I need to press the enter key for different textboxes and their related buttons.
Attachment 179368
These textboxes are not related to each other. I often use this statement if there is only one button:
Attachment 179369
How can I do this?
Printable View
Hey. I need to press the enter key for different textboxes and their related buttons.
Attachment 179368
These textboxes are not related to each other. I often use this statement if there is only one button:
Attachment 179369
How can I do this?
This may be what you want:
https://docs.microsoft.com/en-us/dot...handles-clause
you may try something like :
VB.net 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
TysonLPrice, Delaney and paul. Many thanks. Much appreciated.
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