16

In my application, I have a form that contains a browser control in which I display an SSRS report. I would like to prevent the user from right-clicking in the browser control and being shown the popup menu. Ideally I'd like the right-click to do nothing. Is there a way I can accomplish this?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Randy Minder
  • 47,200
  • 49
  • 204
  • 358
  • 1
    Is this windows forms or WPF? Is this the WebBrowser control or the Report control? – John Saunders Dec 20 '10 at 20:32
  • If you have implemented a browser control, just to show a report, in a SSRS web-control, why don't you use Windows Report control and avoid browser control in this scenario (if its just like that)! –  Dec 20 '10 at 20:38

5 Answers5

26

You can set the IsWebBrowserContextMenuEnabled equal to false. You will probably also want to set AllowWebBrowserDrop equal to false too so they cant drag a url into the app and have it load.

        webBrowser1.IsWebBrowserContextMenuEnabled = false;
        webBrowser1.AllowWebBrowserDrop = false;
Chris Kooken
  • 32,730
  • 15
  • 85
  • 123
  • 3
    Works for the Winforms WebBrowser, not the WPF version. – Hans Passant Dec 20 '10 at 21:10
  • The WPF version seems to be chiefly underfeatured. I personally end up using Forms' WebBrowser through a WindowsFormsHost control almost every time. – Kaganar Jul 01 '13 at 20:42
  • 1
    @Kaganar Only problem with that approach is you're limited to something like IE 5/6-level capabilities for the HTML renderer, unless you set up registry entities to tell IE to use a later version like 10/11 when your app plug into ieframe.dll. – KeithS Jan 20 '16 at 03:08
4

for any case, winform or wpf:

     private void WebBrowser_LoadCompleted(object sender, System.Windows.Navigation.NavigationEventArgs e)
        {
            ((WebBrowser)sender).InvokeScript("eval", "$(document).contextmenu(function() {    return false;        });");
}
  • This is sort of the best answer, as the most elegant one (https://stackoverflow.com/a/21699086/592212) has problems (at least I've experienced crashes), when WebBrowser (WPF) was included in custom control. Requires jQuery thought. – Miro Hudak Jul 17 '17 at 16:29
  • works perfectly, without requiring any changes to either the loaded page or application's own design or registry :) – NitinSingh Jul 26 '17 at 13:45
2

For WPF ===>

wbBrowser.ContextMenu.IsEnabled = false;

  • This works only if you assign first a ContextMenu, otherwise it is null by default. The question is about preventing context menu from the displayed web page, not this WPF context menu. – Alexandru Dicu May 15 '21 at 11:19
1

Set the IsWebBrowserContextMenuEnabled property to false.

Sam B
  • 2,441
  • 2
  • 18
  • 20
0

I have a solution work on my project (WPF - MVVM):

Important add reference: Microsoft.mshtml

Implement event: webBrowser.LoadCompleted += webBrowser_LoadCompleted;

using mshtml;

private void webBrowser_LoadCompleted(object sender, System.Windows.Navigation.NavigationEventArgs e)
{
    object doc = webBrowser.Document;
    HTMLDocumentEvents2_Event evn2 = doc as HTMLDocumentEvents2_Event;
    HTMLDocumentEvents_Event evn = doc as HTMLDocumentEvents_Event;
    evn.oncontextmenu += new HTMLDocumentEvents_oncontextmenuEventHandler(Evn_oncontextmenu);
    evn2.oncontextmenu += new HTMLDocumentEvents2_oncontextmenuEventHandler(Evn2_oncontextmenu);
}

private bool Evn2_oncontextmenu(IHTMLEventObj pEvtObj)
{
    return false;
}

private bool Evn_oncontextmenu()
{
    return false;
}

Hope helpful.