WebBrowser control not auto-selecting text on double-click after IE 10 install

假装没事ソ 提交于 2019-12-01 04:43:28

The problem is actually a bug in IE 10. This was confirmed by Microsoft here.

The saddest part is that they said they would not be fixing it any time soon (if ever) because it affects too few people (though it does affect thousands of my company's customers).

Whilst this has been reported as a known issue in IE10 (none of the links reporting the bug that I've seen still work), it has not been fixed in any more recent versions (hence we're coming across the issue when finally migrating away from WinXP/IE8 to Win10/IE11).

Interestingly the best work around that I've found was from @John's posting of the same problem on the Chinese DataZX forum.

Basically the issue arises when certain properties of the System.Windows.Forms.WebBrowser control are set from the designer within a UserControl which makes its way into the UserControl.Designer.InitializeComponent() method. Moving these property assignments to occur later in the initialization process, eg: into the UserControl.Load event handler, resolves the problem and the double (+ triple) click behaviour of the control is restored.

Known properties causing this problem include (but others may also cause this or similar problems)

  • ScriptErrorsSuppressed
  • AllowWebBrowserDrop

So if either of these properties are set in the designer, move them to the Load event handler and the double click should now work as expected.

Eg (Problem code from UserControl.designer.vb)

Private Sub InitializeComponent()
    Me.WebBrowser1 = New System.Windows.Forms.WebBrowser()
    Me.SuspendLayout()
    '
    'WebBrowser1
    '
    Me.WebBrowser1.AllowWebBrowserDrop = False 'This line will cause the problem
    Me.WebBrowser1.Location = New System.Drawing.Point(20, 3)
    Me.WebBrowser1.MinimumSize = New System.Drawing.Size(20, 20)
    Me.WebBrowser1.Name = "WebBrowser1"
    Me.WebBrowser1.ScriptErrorsSuppressed = True  'This line will ALSO cause the problem
    Me.WebBrowser1.Size = New System.Drawing.Size(147, 138)
    Me.WebBrowser1.TabIndex = 0
End Sub

Comment out the lines above that cause the problem and replace with the following in the UserControl.vb

Private Sub UserControl_Load(sender As Object, e As EventArgs) Handles Me.Load
    'Setting these properties here does not cause any problems
    Me.WebBrowser1.AllowWebBrowserDrop = True 
    Me.WebBrowser1.ScriptErrorsSuppressed = False
    Me.WebBrowser1.DocumentText = "These are test words that you can attempt to double click"
End Sub

I know this was asked a while back, but I too faced the same issue when I created a HTML Editor control and had IE 10 installed.

Here's how I resolved this in C#.Net:

// Add this to your Web Browsers ProgressChanged event
private void web_ProgressChanged(object sender, WebBrowserProgressChangedEventArgs e)
{
    // Detach the event handler before attaching
    web.Document.DetachEventHandler("ondblclick", Document_DoubleClick);

    // Now attach the event handler
    web.Document.AttachEventHandler("ondblclick", Document_DoubleClick);
}

// Add this to your project
private void Document_DoubleClick(object sender, EventArgs e)
{
    IHTMLSelectionObject sel = doc.selection as IHTMLSelectionObject;
    IHTMLTxtRange rng = sel.createRange() as IHTMLTxtRange;
    rng.expand("word");
    rng.select();
}

Now the double click will highlight the word under the mouse.

The WebBrowser control does not play well with other controls, it's needs to be encapsulated (contained within) a Panel, isolated from other form controls.

I was using the WebBrowser control in a Tab Control and had the same issue. When adding a Panel within the tab then placing the WebBrowser within the panel, the issue went away.

I'm late, but someone else may still be asking for help. I have this HtmlEditorControl and I wanted the DoubleClick works on Windows 10. In your WebBrowser.DocumentCompleted method add this code:

this.editorWebBrowser.Document.AttachEventHandler("ondblclick", (_object, _event) =>
        {
            //First check if the user has selected something, if not, move the current selection starting from the current location of the text cursor.
            if (string.IsNullOrEmpty(SelectedText.Trim()))
            {
                //All objects bellow are referenced, there is no need to assign them to the HtmlEditorControl again
                // define the selected range object
                mshtml.IHTMLSelectionObject selection;
                mshtml.IHTMLTxtRangerange = null;
                try
                {
                    // calculate the text range based on user selection
                    selection = _mshtmlDocument.selection;

                    if (selection.type.ToLower().Equals("text") || selection.type.ToLower().Equals("none"))
                    {
                        range = (mshtml.IHTMLTxtRange)selection.createRange();
                    }
                }
                catch (Exception)
                {
                    // have unknown error so set return to null
                    range = null;
                    return;
                }

                if (range == null)
                {
                    //error!!
                    return;
                }

                //Move the current selection one word back
                range.moveStart("word", -1);
                range.moveEnd("word", 1);
                range.select();
                if (range.text == null)
                {
                    //error!!
                    return;
                }
            }
        });

I know it might be rather late to offer a solution, but I ended up modifying and rewriting a few javascript codes that I have stumbled upon and ended up making this, which hopefully may be useful for you.

This should help if anyone is still having this issue.

It is a cross browser, implementation as I work with a few other libraries for different browsers, but should be easy to remove the unnecessary code if you just want IE.

If you do try to implement this, I would recommend just minifying it and creating a js file in your application resources and then referencing it in the injection.

 Dim headElement As HtmlElement = browser.Document.GetElementsByTagName("head")(0)
        Dim scriptElement As HtmlElement = browser.Document.CreateElement("script")
        Dim element As IHTMLScriptElement = DirectCast(scriptElement.DomElement, IHTMLScriptElement)
        element.text = My.Resources.select_text_ie
        headElement.AppendChild(scriptElement)

This should be injected on DocumentCompleted event.

Environment: VS2017 C#, .net framework 4.6, Win10. IE11 is installed. No manipulation on the FEATURE_BROWSER_EMULATION at the registry.

Drop a panel on a form, and then put a WebBrowser control on the panel. Double clicking on any element will not select the text on the html page.

If you don't use a panel and drop the WebBrowser directly on the form, it works fine. What if you need to use it with panels?

Create a simple user control with a WebBrowser on it, and exposed required events and properties of the WebBrowser. Put this user control on any panel and selecting text with double click will work as the way it should be.

Insert <!DOCTYPE html> at the start of your file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!