VB.net文本框返回命中(输入)事件

时间:2012-06-13 08:24:14

标签: asp.net vb.net

如何在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

3 个答案:

答案 0 :(得分:2)

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

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

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

我刚刚在新文本框中检查了该代码并且工作正常。

答案 1 :(得分: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

答案 2 :(得分: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