vb.net在文本框中按箭头键

时间:2014-03-18 11:17:22

标签: vb.net

在文本框内,我可以获得输入和转义按键,但我无法按下箭头(向上向下)按键。相反,它将光标移动到文本框中。我想我必须覆盖这个但是如何?我需要能够仅在文本框内部时才能捕获这些事件 ....所以表单监听器是不可能的(?)

Private Sub txtMytext_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles Mytext.KeyPress

    If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
        MsgBox("Enter pressed!")
    End If

    If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Escape) Then
        MsgBox("Escape pressed!")
    End If

    If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Up) Then
        MsgBox("Up key pressed!") ' this never fires :(
    End If

End Sub

2 个答案:

答案 0 :(得分:2)

在得到DatVM和MrPaulch的有益评论之后,

回答了我自己的问题:

而不是KeyPress我使用了KeyDown事件:

Private Sub txtMyText_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles txtMyText.KeyDown

    If e.KeyCode = Keys.Up Then
        MsgBox("UP")
    End If

End Sub

答案 1 :(得分:0)

您可以尝试:

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.Left) And txtMytext.Focused = True Then
        'your code
    End If
    Return MyBase.ProcessCmdKey(msg, keyData)
End Function

瓦尔特