I\'m using webbrowser control in my winforms app (c#). And when it is doing automation things, I\'m losing focus control from the window I was working with. Webbrowsers\' fo
I guess WebBrowser
acquires the focus after a page is loaded by calling Navigate
(or the Click
method of an HtmlElement
, which causes navigation). The focus could be given back to the control on the window (the TextBox
) in the DocumentComplete
event handler of the WebBrowser
, but this is very difficult:
When would you determine which control owned the focus
originally? Before calling Navigate
? This is not enough, because the
user can move to another control after calling Navigate
, but before
handling DocumentComplete
.
AFAIK setting the focus to a TextBox
will select its whole
content, so you will have to put the cursor back to its original
position. But when would you store the original position? Same problem.
There can be more than one DocumentComplete event after a single
Navigate
(or Click
).
A possible solution would be to create a separate application for your hidden WebBrowser
. This second application would be invisible, and could communicate with the original GUI application using some InterProcess Communication (IPC) technique. Because the WebBrowser
in this case would run in a different process, you would have a better chance not to lose lose the focus and bother the user.