Rick Strahl's Weblog  

Wind, waves, code and everything in between...
.NET • C# • Markdown • WPF • All Things Web
Contact   •   Articles   •   Products   •   Support   •   Advertise
Sponsored by:
Markdown Monster - The Markdown Editor for Windows

Need a little help with Web Browser Control editing - Focus issues


:P
On this page:

The HTML Editing support in Help Builder will be the end of me <g>. The issues just never seem to end. Just when I get one or the other licked another pops up. Aaaargh.

 

I’m pretty happy with where the HTML Editing support in Help Builder has gone at this point. It’s quite workable and even a text based bigot like myself will find himself more and more frequently using visual editing for topics that have a lot of visual aspects (mostly tables and images and special indentations etc.) elements.

 

However, I have one issue that I have been struggling with for a LONG time and cannot resolve, and maybe somebody has some insight into this. The problem has to do with focus of the edit control.

 

I’m using the standard Web Browser control and I use

 

Browser.Document.Body.ContentEditable = "true"

 

to get the document into designmode. There are a bunch of event hookups that deal with menu and key and click trapping. Everything works well inside of the application itself and focusing properly works. I can move to other controls and back, even other windows and back and when I come back to the Web browser in Edit mode focus properly returns.

 

However, when I click or ALT-Tab on another application while focus is on the Web Browser and then return back to Help Builder, the Edit control looses its edit focus: The cursor is invisible and any text typed is not going anywhere.

 

It turns out that the browser IS in fact getting focus back – I’m trapping keystroke events on the document and I can see those events firing, but the control doesn’t activate in edit mode until I physically click the mouse in the client area.

 

From a usability POV this suck big time as it forces you to pick up the mouse in order to find your spot again. I do this fairly frequently when documenting where you flip back and forth between the application your documenting and the text I’m editing.

 

So, problem #1 is that the Edit focus doesn’t properly return to the control automatically. What’s worse though is that I haven’t even been able to programmatically reset the focus either. I’ve tried a ton of things in this respect:

 

  • Setting a selection and selecting it (I can see it but it’s not ‘active’)
  • Focusing the form, the control the document the active area
  • resetting DesignMode and ContentEditable flags
  • Sending WM_ACTIVATE messages, WM_KILLFOCUS messages etc.

 

None of this has worked. I cannot figure out how to get the control back into edit focus.

 

My last resort has been to using WinAPI SendMessage calls to try to send messages to the various controls that make up the Web browser control (there are 3: The ActiveX container, the document and IE Host container) and sending various messages to it. However, I’ve only been guessing at which messages to send and in which order. Even looking at the SPY++ message trapping yielded little reproducible message sequences that I could use.

 

Does anybody have any idea how to get the Web Browser control back into Edit mode with a cursor ready and for entry programmatically?

 

I've been trying to solve this problem for a few weeks now off and on and I'm getting a bit desperate...

 

Any help would be appreciated.


The Voices of Reason


 

wOOdy
March 29, 2005

# re: Need a little help with Web Browser Control editing - Focus issues

What about a plain MOUSE CLICK AT x,y PIXEL ? Does this reactivate the editor? You may stil end up with a misplaced cursor then, but maybe you could save the current coordinates in LostFocus ?


Rick Strahl
March 29, 2005

# re: Need a little help with Web Browser Control editing - Focus issues

Woody, how would you do that exactly? <g> I've done a little searching around trying to find a way to simulate a mouse click with SendMessage() but it doesn't seem like this is something that Windows supports easily.

Gérald
March 30, 2005

# re: Need a little help with Web Browser Control editing - Focus issues

Not sure, but may be this is THE solution?...
http://xstandard.com/


Rick Strahl
March 30, 2005

# re: Need a little help with Web Browser Control editing - Focus issues

I played with XStandard for a while, but this control renders very differently than IE and doesn't support complex document manipulation programmatically. Since I need to write lots of custom markup into the document this is one feature I can't do without.

wOOdy
March 30, 2005

# re: Need a little help with Web Browser Control editing - Focus issues

Hi Rick,
as far as my understanding of HelpBuilder goes, it's written in VFP. And one of the VFP commands is MOUSE. And since you are hosting the Webbrowser Control in a VFP form, VFP can issue a MOUSE CLICK AT a specific position. I just tested it, and unfortunately, while the mouse events are generated as expected on regular VFP controls, the ActiveX doesn't see them. If you do a ON KEY LABEL F11 MOUSE CLICK and move your mouse around specific areas, then press F11 and watch in the degugger where the events are generated, then the mouse click is seen by SCREEN if you are over an ActiveX control. Which is kind of understandable, because an activex is actually another windows process "hovering over" your vfp form, and the formhandler just sees a hole in his form.
Ok, scrap my idea :(


Neil T
March 30, 2005

# re: Need a little help with Web Browser Control editing - Focus issues


Rick Strahl's WebLog
April 01, 2005

# Web Browser HTML Editing Focus issue resolved finally

Getting the Web Browser Controls' Edit features to properly focus when hosted inside of Visual FoxPro is a bit of hassle, but after all the hassles I'd gone through the solution is relatively simple using a delayed focus assignment operation.

Tejendra
October 28, 2005

# re: Need a little help with Web Browser Control editing - Focus issues

When I press Back space or ALT+Left Arrow, then Web Browser Control goes back to previous page. I don't wan't any thing on that event.
How to handle key events in Web Browser Control?

Rick Strahl
October 28, 2005

# re: Need a little help with Web Browser Control editing - Focus issues

You can handle the document's OnKeyDown event and cancel operation there.

Takes a bit of work to do this - you have to implement the event interface on the document

HTMLTextContainerEvents IN "MSHTML.TLB"


Jijo Varghese
November 16, 2005

# re: Need a little help with Web Browser Control editing - Focus issues

Hi,

Am also facing the similar problem with an activex control in my asp page. Once a message box (alert window) is displayed my activex control loses focus and further keystrokes from the users side are not recognised. can any one help..

Thanks,
Jijo

Mark Milley
December 16, 2005

# re: Need a little help with Web Browser Control editing - Focus issues

Hi Rick -

I don't have the time at the moment to test this theory, but have you tried setting the document (body) to UNSELECTABLE?

You have to do this with toolbar buttons on your document, for instance, to prevent a selection from being lost while executing commands (bold, etc).

It's possible that what's happening is that Windows is giving the document as a whole the focus, clearing the current selection.

Shrug... it's worth a try.

Anybody know how to trap an event when an element with contenteditable loses focus or is updated?

-Mark

Mark Milley
December 16, 2005

# re: Need a little help with Web Browser Control editing - Focus issues

(By trap, I mean handle. Every time I give an element contenteditable, either none of my events fire, or the content is no longer editable or both.)

Thanks,

-Mark

Munir Hussain
February 15, 2006

# re: Need a little help with Web Browser Control editing - Focus issues

Hi, Do any one know about the COM Control HTMLEditorControl. How to handle its Key Events, Supress the built-in Context Menu and inter-relate its InnerText and InnerHTML Properties?

kevin delafield
July 19, 2006

# re: Need a little help with Web Browser Control editing - Focus issues

try this:

webBrowser.Document.Body.Focus();

it worked for me

Dennis Look
October 15, 2006

# re: Need a little help with Web Browser Control editing - Focus issues

Thanks for the code Kevin. That fixes a problem that's been irking me for too long now. I've been trying to find a way to focus the webbrowser control properly without causing other problems. Everything else I've tried has caused a new bug or I'd lose functionality in a new area. Sometimes the smallest additions make all the difference.

Andreas Dietrich
November 29, 2006

# re: Need a little help with Web Browser Control editing - Focus issues

Thanks to Kevin.

He greatly helped me stopping banging head on my desktop

Jeff
October 27, 2008

# re: Need a little help with Web Browser Control editing - Focus issues

I'm not sure if I'm having the same issue with the WebBrowser control in .NET but it looks similar. See my post on the MSDN forums below.

<a href="http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/1b50fec6-6596-4c0a-9191-32cd059f18f7">Focus issues with System.Windows.Controls.WebBrowser</a>
http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/1b50fec6-6596-4c0a-9191-32cd059f18f7

Greg
February 04, 2009

# re: Need a little help with Web Browser Control editing - Focus issues

Kevin's bang on ...
I've made a few apps that have required extensive switching thru user or code and whenever I've wanted immediate results afterwards, I needed to reference the DOM of the contained form rather then the form object itself.

West Wind  © Rick Strahl, West Wind Technologies, 2005 - 2024