VB 禁用“|”钥匙

VB disable "|" key

我正在尝试禁用 |在文本框中键入,因为我的 csv 文件使用该字符作为分隔符。但是,我尝试了很多方法并在 google 上进行了搜索,但找不到与此相关的任何解决方案。

我的代码:

    Private Sub txtRemarks_KeyDown(sender As Object, e As KeyEventArgs) Handles txtRemarks.KeyDown
        If e.KeyCode = Keys.Shift And e.KeyCode = AscW(220) Then
            e.SuppressKeyPress = True
        End If
    End Sub

想知道这是否可能?谢谢

如果你想检测一个键与一个或多个修饰符的组合,那么有多种方法可以做到这一点。你可以这样做:

Private Sub txtRemarks_KeyDown(sender As Object, e As KeyEventArgs) Handles txtRemarks.KeyDown
    If Not e.Control AndAlso e.Shift AndAlso Not e.Alt AndAlso e.KeyCode = Keys.Oem5 Then
        e.SuppressKeyPress = True
    End If
End Sub

或者这个:

Private Sub txtRemarks_KeyDown(sender As Object, e As KeyEventArgs) Handles txtRemarks.KeyDown
    If e.Modifiers = Keys.Shift AndAlso e.KeyCode = Keys.Oem5 Then
        e.SuppressKeyPress = True
    End If
End Sub

但我的偏好是:

Private Sub txtRemarks_KeyDown(sender As Object, e As KeyEventArgs) Handles txtRemarks.KeyDown
    If e.KeyData = (Keys.Shift Or Keys.Oem5) Then
        e.SuppressKeyPress = True
    End If
End Sub

请注意,第三个代码段使用 KeyData 而不是 KeyCode。后者只是当前键,而前者是当前键和任何修改键。

如果你想让它更简洁一点:

Private Sub txtRemarks_KeyDown(sender As Object, e As KeyEventArgs) Handles txtRemarks.KeyDown
    e.SuppressKeyPress = (e.KeyData = (Keys.Shift Or Keys.Oem5))
End Sub