views:

114

answers:

1

I am using an EventHandler to suppress a context menu on an AxWebBrowser. I am using the code below:

public class HtmlEditor : System.Windows.Forms.UserControl
{
    ...
    ...
    private AxSHDocVw.AxWebBrowser axWebBrowser1;
    ...
    ...
    public HtmlEditor()
    {
        InitializeComponent();
        axWebBrowser1.DocumentComplete +=new AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEventHandler(axWebBrowser1_DocumentComplete);
        axWebBrowser1.Navigate("about:blank");
        axWebBrowser1.BeforeNavigate2 +=new AxSHDocVw.DWebBrowserEvents2_BeforeNavigate2EventHandler(axWebBrowser1_BeforeNavigate2);
        mshtml.HTMLDocumentClass doc = (mshtml.HTMLDocumentClass)this.axWebBrowser1.Document;///stop here
        doc.designMode = "On";  // This turns the control into an editor ///stop here
        ((mshtml.HTMLDocumentEvents2_Event)this.axWebBrowser1.Document).oncontextmenu += new mshtml.HTMLDocumentEvents2_oncontextmenuEventHandler(HtmlEditor_oncontextmenu); //suppress context menu ///stop here
        Dirty = false;///stop here
    }

    private bool HtmlEditor_oncontextmenu(mshtml.IHTMLEventObj e)
    {
        return false;
    }
    ...
    ...
}

There are four lines with the comment "///stop here". If I set a breakpoint at any one of those lines, run the app, then continue after I stop at that line, then the context menu does not appear when I right-click the control. However, if I put a breakpoint anywhere else and do the same, the context menu appears. Additionally, if I run the app with no breakpoints, the context menu appears.

I have also tried to assign the EventHandler in a separate method, called long after everything above is done executing, but again, the context menu appears.

Anyone have an idea how I can figure out my problem?

A: 

You should use the WinForms WebBrowser control and set its IsWebBrowserContextMenuEnabled property to false.

You can use its Document.DomDocument to get the raw MSHTML HTMLDocument object.

SLaks
Thanks for the suggestion, but I unfortunately can't change the control type to WebBrowser.
Jerry
Since the AxWebBrowser isn't exposed by the control, why can't you change it to WebBrowser?
SLaks
I believe the only reason is that the WebBrowser.Document needs to be editable. AxWebBrowser makes it easy to edit the Document. I'm sure a Document in WebBrowser could be made editable (found part of a solution at http://social.msdn.microsoft.com/forums/en-US/Vsexpressvcs/thread/7e6444bd-8dbc-489b-8f70-622aef381741), but I am going to have to do a full review to make sure changing to WebBrowser doesn't break any other code.
Jerry