Search code examples
vb.netvisual-studiowebbrowser-controlcontextmenumenuitem

VB.Net Context Menu items click not firing in the web browser


I'm working on the context menu items to get the messagebox to display when I click on the context menu items. The click event for the context menu items will not be firing when I create custom right mouse click event for the web browser.

I have tried a few number of ways to get it to work and none of them will works, it keep going around the circle as nothing will happens when I click on the context menu items.

Here is what I have tried:

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

And I have also tried this:

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

Do you know why the context menu items will not be firing when I click on the context menu items?

If I did something wrong, can you please show me an example what is the correct way to get the context menu items to firing when I click on the items after I click on the right mouse button on the web browser?


Solution

  • In the first code, the context menu item didn't trigger because you didn't set the property IsWebBrowerContextMenuEnabled in WebBrowser1 to false.

    enter image description here

    I have tested the second code and found it feasible. Can you provide more information about your projects and operations? So that I can help you solve it.