Detecting Enter keypress on VB.NET

前端 未结 15 1309
后悔当初
后悔当初 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:04

    There is no need to set the KeyPreview Property to True. Just add the following function.

    Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, _
                                               ByVal keyData As System.Windows.Forms.Keys) _
                                               As Boolean
    
        If msg.WParam.ToInt32() = CInt(Keys.Enter) Then
            SendKeys.Send("{Tab}")
            Return True
        End If
        Return MyBase.ProcessCmdKey(msg, keyData)
    End Function
    

    Now, when you press Enter on a TextBox, the control moves to the next control.

提交回复
热议问题