0

我目前正在 VB.net 中构建一个 Web 浏览器,但我很难e.SuppressKeyPress = True开始工作。我在我的 URL 栏上使用此代码,因此用户可以按 Enter 并导航到所需的页面。我的整个代码:

    Private Sub AddressBar_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles AddressBar.KeyUp
    On Error Resume Next
    If e.KeyCode = 13 Then
        Dim textArray = AddressBar.Text.Split(" ")
        If (AddressBar.Text.Contains(".") = True And AddressBar.Text.Contains(" ") = False And AddressBar.Text.Contains(" .") = False And AddressBar.Text.Contains(". ") = False) Or textArray(0).Contains(":/") = True Or textArray(0).Contains(":\") Then
            Navigate(AddressBar.Text)
        Else
            Navigate("http://www.google.com/search?q=" + AddressBar.Text)
        End If
        WebControl.Focus()
        e.SuppressKeyPress = True
    End If
End Sub

我曾尝试将其移动e.SuppressKeyPress = True到多个不同的地方,但它们都不起作用。有什么建议么?

4

1 回答 1

0

您不能在 KEYUP 事件中抑制密钥,该密钥已被处理。微软甚至给你这个选项是相当愚蠢的。在示例应用程序中尝试此代码并比较结果

Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
    If e.KeyCode = Keys.A Then
        e.SuppressKeyPress = True
    End If
End Sub
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
    If e.KeyChar = "b" Then
        e.Handled = True
    End If
End Sub
Private Sub TextBox1_KeyUp(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyUp
    If e.KeyCode = Keys.C Then
        e.SuppressKeyPress = True
    End If
End Sub
于 2013-10-24T16:35:09.393 回答