WebBrowser control does not automatically select text when double-clicked after installing IE 10

NOTE. This is about the inline behavior of the control, not the creation of a double-click event.

One of my projects uses the Windows.Forms.WebBrowser control as an editor and viewer for HTML. It has been working fine for many years, but suddenly I noticed that earlier, when I double-clicked, it would automatically select the word that was pressed (it was not my code, it just happened to the control).

Suddenly (not entirely accurate), double-clicking on the text in the control (both in edit mode and in view mode) does nothing. It’s not easy to troubleshoot because it’s not my code, however, since in the past I depended on this functionality, now it affects my final product.

Any idea that has recently changed (on Windows or .Net) that would affect this (the same version of my application works fine in the old version of Win7, but not in the latest version of Win7)? Also, how can I revert to previous functionality? Do I have to plug in my own double click even in the DOM and then start parsing the text to manually select it (uh!)?

I looked through the entire answer to this question, but basically I just see how to connect to DOM events, nothing about the latest changes that could cause the problem above). Any help would be greatly appreciated.

I use VS 2010, VB, Win7 x64, IE 10.0.6, .Net 3.5 (regardless of the latter) when the problem occurs (compiled, so I doubt that it is VS or VB).

When I run the same code (compiled) on Win7 x86 with IE 8 (simple Win7 Pro with zero updates installed), it works fine.

After some additional testing on the new version of Win7 x86 (where everything was fine), as soon as I installed IE 10 and NOTHING ELSE, the problem starts to arise. So, I'm sure the problem is with IE 10.

Steps to play:

  • Create a new VB.Net project in VS 2010 by customizing .Net 3.5
  • Create usercontrol <--- this step is the key
  • Add web browser to usercontrol
  • Add this code to usercontrol

    Public Sub LoadHTML(html As String) WebBrowser1.DocumentText = html Do Until WebBrowser1.ReadyState = WebBrowserReadyState.Complete Application.DoEvents() Loop WebBrowser1.Document.Body.SetAttribute("contentEditable", "false") End Sub 
  • Add this control to the form.

  • Add a button to the form with the next button. Click event:

     Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click UserControl1.LoadHTML("<html><body>This is a test interesting is it not?</body></html>") End Sub 
  • Run the project and click the button

  • Then try double-clicking the word "interesting" and notice that it does not select.

If you drop the webbrowser control directly into the form, it will work fine. The problem with usercontrol (and this is only a problem after upgrading to IE10).

I can reproduce the problem using VS 2010 using .Net 3.5 and 4.0. I can reproduce the problem using VS 2012 using .Net 4.5.

+6
source share
7 answers

The problem is actually a bug in IE 10. It has been confirmed by Microsoft here .

The saddest part is that they said that they will not fix it in the near future (if ever), because it affects too few people (although it affects thousands of my clients).

+3
source

Although this was reported as a known issue in IE10 (none of the links reporting the error I saw still work), it was not fixed in later versions (so we encounter a problem when we finally move from WinXP / IE8 to Win10 / IE11).

Interestingly, the best work around what I found was from @John posting the same issue in the Chinese DataZX forum.

Basically, the problem occurs when certain properties of the System.Windows.Forms.WebBrowser control are set from the constructor inside UserControl , which breaks into the UserControl.Designer.InitializeComponent() method. Moving these property assignments occurs later in the initialization process, for example: into the UserControl.Load event handler, eliminates the problem and restores the behavior of double (+ triple) clicking the control.

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

  • ScriptErrorsSuppressed
  • AllowWebBrowserDrop

So, if any of these properties are set in the constructor, move them to the load event handler, and double-clicking should work as expected.

For example (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 are causing the problem and replace them as follows: 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 
+2
source

I know this was suggested some time ago, but I also ran into the same problem when I created the HTML Editor control and installed IE 10.

Here is how I solved it 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 double-clicking will highlight the word under the mouse.

+1
source

The WebBrowser control does not work well with other controls; it needs to be encapsulated (contained inside) by a panel isolated from other form controls.

I used the WebBrowser control in Tab Control and had the same problem. When adding a panel inside a tab, placing the WebBrowser inside the panel, the problem disappeared.

0
source

I'm late, but someone else might ask for help. I have an HtmlEditorControl and I wanted DoubleClick to work 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; } } }); 
0
source

I know that it can be quite late to propose a solution, but I ended up changing and rewriting a few javascript codes that I came across, and ended up doing this, which I hope can be useful to you.

This should help if someone still has this problem.

This is a cross browser implementation because I work with several other libraries for different browsers, but it should be easy to remove unnecessary code if you just want IE.

If you try to implement this, I would recommend just minimizing it and creating a js file in your application resources, and then referring to it during the injection process.

  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 into the DocumentCompleted event.

0
source

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

-2
source

Source: https://habr.com/ru/post/948828/


All Articles