Detecting Enter keypress on VB.NET

前端 未结 15 1277
后悔当初
后悔当初 2020-12-05 04:56

I am using .NET 3.5 framework of VB.NET 2008.

I have some textboxes in my form. I want the tab-like behavior when my user presses ENTER on one of my textboxes. I use

相关标签:
15条回答
  • 2020-12-05 05:20

    The following code will work.

    Public Class Form1
        Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
            If e.KeyChar = Convert.ToChar(13) Then
                MsgBox("enter key pressd ")
            End If
        End Sub
    End Clas
    
    Public Class Form1
        Private Sub TextBox1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
            If e.KeyCode = Keys.Enter Then
                MsgBox("enter key pressd ")
            End If
        End Sub
    End Class
    
    0 讨论(0)
  • 2020-12-05 05:23
    Private Sub SomeTextBox_KeyPress(sender As Object, e As KeyPressEventArgs) Handles SomeTextBox.KeyPress
    
        If Asc(e.KeyChar) = 13 Then
             MessageBox.Show("Enter pressed!")
             e.Handled = True
        End If
    
    End Sub
    
    0 讨论(0)
  • 2020-12-05 05:24

    I'm using VB 2010 .NET 4.0 and use the following:

    Private Sub tbSecurity_KeyPress(sender As System.Object, e As System.EventArgs) Handles tbSecurity.KeyPress
        Dim tmp As System.Windows.Forms.KeyPressEventArgs = e
        If tmp.KeyChar = ChrW(Keys.Enter) Then
            MessageBox.Show("Enter key")
        Else
            MessageBox.Show(tmp.KeyChar)
        End If
    
    End Sub
    

    Works like a charm!

    0 讨论(0)
提交回复
热议问题