VB.Net 上下文菜单项在 Web 浏览器中单击不触发

问题描述 投票:0回答:1

我正在处理上下文菜单项,以便在单击上下文菜单项时显示消息框。当我为 Web 浏览器创建自定义鼠标右键单击事件时,上下文菜单项的单击事件将不会触发。

我尝试了几种方法让它工作,但没有一个能工作,它一直在绕圈,因为当我单击上下文菜单项时什么也不会发生。

这是我尝试过的:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim html As String = "<!DOCTYPE html><html><head><script src='https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js'></script></head><body><div id='main' class='main' style='padding-left: 13px;'><div id='editor' dir='auto' placeholder='' aria-multiline='true' contenteditable='true' tabindex='1'></div></div></body></html>"

    WebBrowser1.DocumentText = html
    WebBrowser1.ContextMenu = contextMenu1
End Sub

Private Sub MenuItem4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handler MenuItem4_Click
    MessageBox.Show("Copy")
End Sub

我也尝试过这个:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim html As String = "<!DOCTYPE html><html><head><script src='https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js'></script></head><body><div id='main' class='main' style='padding-left: 13px;'><div id='editor' dir='auto' placeholder='' aria-multiline='true' contenteditable='true' tabindex='1'></div></div></body></html>"

    WebBrowser1.DocumentText = html
End Sub

Private Sub WebBrowser1_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted

    AddHandler WebBrowser1.Document.MouseDown, New HtmlElementEventHandler(AddressOf WebBrowser1_MouseDown)
End Sub



Private Sub WebBrowser1_MouseDown(ByVal sender As Object, ByVal e As HtmlElementEventArgs)

    If e.MouseButtonsPressed = Windows.Forms.MouseButtons.Right Then
        ContextMenu1.Show(WebBrowser1, New System.Drawing.Point(e.MousePosition.X, e.MousePosition.Y))
        AddHandler MenuItem4.Click, New EventHandler(AddressOf MenuItem4_Click)
    End If
End Sub


Private Sub MenuItem4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    MessageBox.Show("Copy")
End Sub

你知道为什么当我点击上下文菜单项时,上下文菜单项不会被触发吗?

如果我做错了什么,您能否向我展示一个示例,当我在网络浏览器上单击鼠标右键后单击项目时,触发上下文菜单项的正确方法是什么?

vb.net visual-studio webbrowser-control contextmenu menuitem
1个回答
0
投票

在第一个代码中,上下文菜单项没有触发,因为您没有将 WebBrowser1 中的 IsWebBrowerContextMenuEnabled 属性设置为 false。

enter image description here

我测试了第二个代码,发现可行。您能否提供有关您的项目和运营的更多信息?这样我才能帮你解决。

© www.soinside.com 2019 - 2024. All rights reserved.