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?
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);
}
}
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.
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With