vb.net 检测是否在 Webbrowser 控件中单击了链接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14225616/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 11:49:58  来源:igfitidea点击:

vb.net Detect if a link is clicked in Webbrowser control

vb.net

提问by eqiz

I have a Webbrowser control on a standard windows form in VB.NET 2005. I just want to detect when someone clicks a link inside the Webbrowser control it just tells me what they clicked on, or where its trying to go, then cancel the process.

我在 VB.NET 2005 中的标准 Windows 窗体上有一个 Webbrowser 控件。我只想检测何时有人单击 Webbrowser 控件内的链接,它只是告诉我他们单击了什么,或者它试图去哪里,然后取消该过程.

I tried putting..

我试过放..

MsgBox(e.Url)
e.Cancel = True

Inside of the WebBrowser1_Navigating EVENT, but that does nothing. Can anyone help?

在 WebBrowser1_Navigating EVENT 内部,但没有任何作用。任何人都可以帮忙吗?

采纳答案by Logi Guna

This was the problem:

这就是问题所在:

MsgBox(e.Url)

MsgBox(e.Url)

Try this:

尝试这个:

MsgBox(e.Url.ToString())

MsgBox(e.Url.ToString())

Private Sub WebBrowser1_Navigating(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserNavigatingEventArgs) Handles WebBrowser1.Navigating
    If MsgBox("You are trying to go to:" & vbCr & e.Url.ToString() & vbCr & "Cancel Navigate?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
        e.Cancel = True
    End If
End Sub

回答by 5uperdan

you could try something like adding a handler for each link:

您可以尝试为每个链接添加一个处理程序:

For Each htmlEle As HtmlElement In Webbrowser1.document.Links
    addhandler htmlElec.click, addressof YourSub
Next

private sub YourSub()
    'do what you want here
end sub