0

如何在 TextBox 上捕获返回命中(输入)?

以下不起作用。我不断收到错误“找不到 KeyPress 事件”。

Private Sub TextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox.KeyPress
        If TextBox.Text Is Nothing Then Return
        If TextBox.Text.Length = 6 And Asc(e.KeyChar) = 13 Then
            ' Do something here
        End If

End Sub
4

3 回答 3

2

尝试删除文本框并重新输入代码

If TextBox.Text Is Nothing Then Return
    If TextBox.Text.Length = 6 And Asc(e.KeyChar) = 13 Then
        ' Do something here
    End If

因为这应该可以工作 - 您很可能对文本框名称或按键声明代码搞砸了。

我刚刚在一个新的文本框中检查了该代码,它运行良好。

于 2012-06-13T08:31:58.993 回答
0

以下代码确实有效:

Private Sub TextBox_KeyPress(ByVal KeyAscii As Integer)

        If TextBox.Text Is Nothing Then Return
        If TextBox.Text.Length = 6 And KeyAscii = 13 Then
            ' Do something here
        End If

End Sub
于 2012-06-13T08:42:43.427 回答
0
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
    If e.KeyChar = Convert.ToChar(13) Then
        If TextBox1.Text.Trim <> Nothing Then
            'Do Something here
        End If
    End If
于 2016-03-29T04:36:33.873 回答