5

I would like to select all when a user right clicks on my web browser control.

I am developing a win forms app, and use web browsers to display my information, because i can use html to style the words.

The right click context menu is not working for me. The options on it are all irrelevant to my app.

But the context menu after a select has been made i want to keep, the copy, cut, paste options.

I can already select all:

getCurrentBrowser().Document.ExecCommand("SelectAll", true, null);

I would just like to do it in the right click event of the web browser?

Grant Winney
  • 65,241
  • 13
  • 115
  • 165
Pomster
  • 14,567
  • 55
  • 128
  • 204

4 Answers4

5

Handle MouseDown event:

webBrowser.Document.MouseDown += new HtmlElementEventHandler(Document_MouseDown);

and make sure user pressed Right button, then select all:

void Document_MouseDown(object sender, HtmlElementEventArgs e)
{
    if(e.MouseButtonsPressed == MouseButtons.Right)
    {
        webBrowser.Document.ExecCommand("SelectAll", true, null);
    }
}
Ria
  • 10,237
  • 3
  • 33
  • 60
4

This article shows how you can replace the context menu of the Web Browser with your own.

Alternatively, if you execute the following Javascript from within the Web Browser, it will disable the default right-click context menu:

    document.oncontextmenu=new Function("return false")

If you are using WinForms rather than WPF, you can set the IsWebBrowserContextMenuEnabled to false to prevent the IE context menu, in which case it will use the ContextMenu you supply on the WebBrowser control.

WPF doesn't have the same property exposed on the Web Browser, so it may not be so easy. In this case you may have to use WindowsFormsHost to host the WinForms Web Browser in WPF.

Michael
  • 8,891
  • 3
  • 29
  • 42
2

This works :)

When the context menu shows select all is running pushing out the contextmenu i want, with the copy, paste, cut and so on.

private void webCompareSQL_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            if (webCompareSQL.Document != null)
            {
                webCompareSQL.Document.ContextMenuShowing += DocMouseClick;
            }
        }
        void DocMouseClick(object sender, HtmlElementEventArgs e)
        {
            webCompareSQL.Document.ExecCommand("SelectAll", true, null);
        }
Pomster
  • 14,567
  • 55
  • 128
  • 204
  • It does not exactly capture the right click event, but the contextMenuShowing will always display when the web browser is clicked. so its a good work around. – Pomster Jul 10 '12 at 07:19
  • what if Document is null?? for PDF webbrowser Document always null .then how you will create click event ? – Tariq_ASp.net Aug 17 '17 at 10:06
0

You need to be sure, that the WebBrowser.Document property is already loaded. Then you can register the Mouse event.

`browser.DocumentCompleted += (s, e) => { 
                             browser.Document.MouseMove += (sM, eM) +=> { 
                                                          Debug.WriteLine(eM.ClientMousePosition.X); 
                                                                        };
                                       };
`