gpt4 book ai didi

vb.net - 无法检测 Enter 键的 KeyPress 事件

转载 作者:行者123 更新时间:2023-12-02 08:39:39 24 4
gpt4 key购买 nike

我尝试在 VB.NET 2008 中使用 KeyPress 事件,但它不起作用。谁能帮我找出这段代码有什么问题吗?此消息框不会出现,数据库中的我的状态也不会出现。程序说它没问题,但它不工作。

If e.KeyChar = Chr(Keys.Enter) Then
tblLogin = Proses.ExecuteQuery("Select * From TblUser where kode_user = '" & KdUserTxt.Text & "'")
If tblLogin.Rows.Count = 0 Then
MessageBox.Show("Kode User Not Found!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
KdUserTxt.Focus()
Else
StatusTxt.Text = tblLogin.Rows(0).Item("status")
PswTxt.Focus()
End If
End If

最佳答案

您不应该使用 KeyPress 事件来捕获控制键(例如 Enter 键)。相反,您应该使用 KeyDown 事件。 KeyDown 允许您捕获键盘上的任何物理键。例如:

Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = Keys.Enter Then
MessageBox.Show("Enter key pressed")
End If
End Sub

但是,完成此类操作的正常方法是向表单添加一个按钮,例如“确定”或“提交”按钮。然后,在表单的属性中,将 AcceptButton 属性设置为该按钮。然后,当用户按下 Enter 键时,WinForm 框架将自动为您调用该按钮的单击事件。同样,CancelButton 属性设置当用户按 Escape 键时单击哪个按钮。

关于vb.net - 无法检测 Enter 键的 KeyPress 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12730391/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com