当前位置 : 主页 > 编程语言 > c语言 >

e.Handled在VB.net 2010中无效

来源:互联网 收集:自由互联 发布时间:2021-06-24
我在vb.net上做了一个快速的网络浏览器,我有它,所以当你按Enter键导航到textbox1中的网页.唯一的问题就是每次按下回车都会发出哔哔声.我尝试使用e.Handled = True,但它没有做任何事情.这是
我在vb.net上做了一个快速的网络浏览器,我有它,所以当你按Enter键导航到textbox1中的网页.唯一的问题就是每次按下回车都会发出哔哔声.我尝试使用e.Handled = True,但它没有做任何事情.这是我的按键代码

Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown

    If e.KeyCode = Keys.Enter Then
        e.Handled = True
        WebBrowser1.Navigate(TextBox1.Text)
    End If

End Sub

我以为e.Handled会让那令人讨厌的嘟嘟声消失,但事实并非如此.

您想要的KeyEventArgs属性不是 Handled而是 SuppressKeyPress.

Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
    If e.KeyCode = Keys.Enter Then
        e.SuppressKeyPress = True
        WebBrowser1.Navigate(TextBox1.Text)
    End If

End Sub

从第一个MSDN链接:

Handled is implemented differently by different controls within Windows Forms. For controls like TextBox which subclass native Win32 controls, it is interpreted to mean that the key message should not be passed to the underlying native control. If you set Handled to true on a TextBox, that control will not pass the key press events to the underlying Win32 text box control, but it will still display the characters that the user typed.

If you want to prevent the current control from receiving a key press, use the SuppressKeyPress property.

网友评论