在KeyDown吃钥匙

时间:2014-03-07 20:50:47

标签: vb.net

Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
    If e.KeyCode = Keys.V Then
        If e.Control = True Then
            e.Handled = True 'eat it
        End If
    End If
End Sub

这不起作用。 有谁知道为什么以及如何以适当的方式实现同​​样的目标?

谢谢!

ps:为什么.Handled属性是可写的,但它没有做任何事情?我想我错过了什么。

2 个答案:

答案 0 :(得分:3)

您应该捕获KeyPress事件而不是KeyDown

Private isCopyPaste As Boolean = False
Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
     isCopyPaste = (e.KeyCode = Keys.V AndAlso e.Control)
End Sub

Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs ) Handles TextBox1.KeyPress
    If isCopyPaste Then
        e.Handled = True 'eat it
    End If
End Sub

另请在MSDN上查看此参考:http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keypress.aspx

答案 1 :(得分:2)

改为使用SuppressKeyPress:

Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
  If e.KeyCode = Keys.V Then
    If e.Control = True Then
      e.SuppressKeyPress = True
    End If
  End If
End Sub