我有一个密码文本框(Tbx1)和一个登录按钮(Btn1)。我会把什么代码放在哪里,以便当用户 输入密码并按回车键,Btn1 clickevents将被执行?
答案 0 :(得分:1)
试试这个:
Private Sub TextBox1_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = Keys.Enter Then
fun()
End If
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
fun()
End Sub
Private Sub fun()
MessageBox.Show("Enter key pressed")
End Sub
答案 1 :(得分:0)
在表单杂项属性上将接受按钮设置为Btn1。同样,只要用户按下Esc按钮,您也可以设置取消按钮。
代码版本(通过构造函数):
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Me.AcceptButton = Btn1
End Sub