2

I have in a desktop application a webpage with a menustrip. I use webview2 as webbrowser control. When i click on the menustrip the menu dropsdown. The problem is that the dropdown doesn’t hide when i click on the webpage. In the standard vb net webbrowser this is not happening. So how can i hide the dropdown menu in webview2?

Maybe the answer lies in VB net. I am looking for a method to close the dropdown of the menustrip of my form i can call than this method from javascript on my webpage

user3025401
  • 181
  • 1
  • 9
  • Add this Javascript when the CoreWebView2 initialization completes: `document.addEventListener('mousedown', function (event) { window.chrome.webview.postMessage('MouseDown'); });`. Then `await [WebView2].CoreWebView2.AddScriptToExecuteOnDocumentCreatedAsync("[The Script String]")`. Add a handler to `WebMessageReceived`, where you check `if e.WebMessageAsJson.Contains("MouseDown")`, loop `[MenuStrip].Items.OfType(Of ToolStripMenuItem)()` and call `[item].HideDropDown()` on each. -- Note that this only works when a page is loaded, if the Control is blank, no event is raised. – Jimi May 24 '21 at 13:07
  • This is still problem in the WinForms Control.. See also: [Webview2.Winforms.DOM](https://github.com/ukandrewc/Webview2.Winforms.DOM) – Jimi May 24 '21 at 13:31
  • @jimi: Don't write such long comments that intent to to answer the question, write an answer! – Poul Bak May 24 '21 at 15:21
  • @PoulBak It's not an answer, it's a *test this ugly thing, see whether it works for you*. I'm not sure it does (the WebView2 version is undefined, as it the UI Platform - yes, MenuStrip invokes WinForms). And, I don't really like it. If I can make it work with something like the CoreWebView2CompositionController or similar, I'll post it. – Jimi May 24 '21 at 17:10
  • Does this answer your question? [How do you override the ContextMenu that appears when right clicking on WebView2 Control?](https://stackoverflow.com/questions/62624373/how-do-you-override-the-contextmenu-that-appears-when-right-clicking-on-webview2) – Poul Bak May 24 '21 at 19:54
  • @Poul Bak No it doesnt my answer. I work with vb net 4.8 and json'object doesnt work in 4.8 . – user3025401 May 25 '21 at 20:45
  • @user3025401: Yes, it does, just download the 'Newtonsoft' package: Select 'project', 'Nuget package' then download 'Newtonsoft' (that's what is used in that answer). – Poul Bak May 26 '21 at 00:36
  • After installation, add `using Newtonsoft.Json;` to your class. – Poul Bak May 26 '21 at 01:21
  • @Jimi That's a pity `WebView2` behaves like that, in addition I want to report more issues: the `MenuStrip` shortcuts/hotkeys will NOT work when the cursor is over the `WebView2` or it has been clicked, the `MenuStrip` doesn't work as intended. – Simple Jul 31 '22 at 22:05

1 Answers1

1

Thanx Jimi and Poul i solved the issue. It was quite simple thnx to you. Solution: 1 my webpage is openlayers map which has a eventlistner for click,

map.on('singleclick', function (event) {
    var lonLat = ol.proj.toLonLat(event.coordinate);
    var str = lonLat[0] + "," + lonLat[1];

(window.chrome.webview.hostObjects.sync.Bridge).Map_click(str);

});

Vb net 2. i use a bridge between vb net and javascript WebView21.CoreWebView2.AddHostObjectToScript("Bridge", New Bridge()) to make a bridge between my vb net desktop application and the javascript on the Webppage.

3 I loop through the menu items and hide the dropdown of the menu item

Private Sub clearmenustrip() For Each mnuitem As System.Windows.Forms.ToolStripMenuItem In Fmenu.MenuStrip1.Items
        mnuitem.HideDropDown()
    Next
End Sub 

MenuStrip1 is the menustrip in the Form

user3025401
  • 181
  • 1
  • 9
  • 1
    This solution does NOT solve the following: the `MenuStrip` shortcuts/hotkeys will NOT work when the cursor is over the `WebView2` or it has been clicked, the `MenuStrip` doesn't work as intended. – Simple Jul 31 '22 at 22:11