简体   繁体   English

在vb.net的文本框中如何设置Enter是可以的

[英]How to Set Enter is ok on Textbox in vb.net

I have a password textbox (Tbx1) and a Login button (Btn1). 我有一个密码文本框(Tbx1)和一个登录按钮(Btn1)。 What code will i put where so that when the user inputs password and presses enter, Btn1 clickevents will be exucuted? 我将在哪里放置什么代码,以便当用户输入密码并按Enter时,将触发Btn1 clickevent?

Try this: 尝试这个:

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

on Form Misc Properties set the accept button to be Btn1. 在“表单其他属性”上,将“接受”按钮设置为Btn1。 Equally, you can set a cancel button too to be executed whenever the user presses Esc button. 同样,您也可以设置一个取消按钮,以便在用户按下Esc按钮时执行。

Code version (through the constructor): 代码版本(通过构造函数):

Public Sub New()

    ' This call is required by the designer.
    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.
    Me.AcceptButton = Btn1
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM