2

I do not want the text in my WebBrowser control to be selectable. How would I do this?

Nosredna
  • 83,000
  • 15
  • 95
  • 122
  • 1
    Well, he may or may not hate his user. I've had to do this a few times in visualizations where the user is moving the mouse to manipulate things on the screen. Believe me, the user is not happy about highlighting in that case. – Nosredna Dec 06 '09 at 22:35

4 Answers4

4

Try this:

// deselects text in _current_ document
private void Document_Uselect(object sender, HtmlElementEventArgs e)
{
    MainWebBrowser.Document.ExecCommand("Unselect", true, null);
}

// if _current_ document changes - we need new handlers
private void MainWebBrowser_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
    MainWebBrowser.Document.Click += new HtmlElementEventHandler(Document_Uselect);
    MainWebBrowser.Document.MouseMove += new HtmlElementEventHandler(Document_Uselect);

}
user620194
  • 41
  • 1
2

May be this link will answer your question. I have not tried this script. But I guess this works. http://www.dynamicdrive.com/dynamicindex9/noselect.htm

VJOY
  • 3,752
  • 12
  • 57
  • 90
1

That is a bit of an unusual request...

You cannot control what is selected or not in the WebBrowser control, unless you want to write one for yourself. What you could do though is stretch a DIV or a transparent image directly over top of the webbrowser control, so any attempt to highlight is actually done on the topmost element, not the webbrowser control.

slugster
  • 49,403
  • 14
  • 95
  • 145
  • Is that true? I know there are two or three ways to do it in Internet Explorer. Is the WebBrowser control that different from IE? – Nosredna Dec 06 '09 at 22:36
0

Though the question was put in 2009, still no simple answer can be found in uncle Google. In Visual Studio 2015 I found, after experiments, the following way to block selection of text in a Webbrowser control.

<body onselectstart="window.event.returnValue=false;">

The same attribute can be applied to any other tag with equal success.

In contrast, neither onselectstart = "return false", onselectstart = "function(){return false}", unselectable="on", nor "-ms-user-select: none;" in CSS appeared to work for me.

Marek
  • 1