Mikkel Nielsen
Mikkel Nielsen

Reputation: 792

Give focus to Internet Explorer object created through C#

I'm having some problems with the control of IE through C#. I have almost everything managed. But I can't seem to set the focus to opened explorer. When I'm in VS 2010 it works, but this isn't the case when I run the exe file directly.

using SHDocVw;
.

<code>

.
InternetExplorer ie = new InternetExplorer();
IWebBrowserApp wb = (IWebBrowserApp)ie;
.

<code>

.   
wb.Visible = true;
wb.Document.focus();

I mean that wb.Document.focus(); would give focus to IE, but that dosen't work. Have also tried with eb.Document.focus(); Anyone have a sugestion?

Upvotes: 0

Views: 1920

Answers (2)

Dalibor
Dalibor

Reputation: 1

It is not reliable. The reliable solution is with API (my discovery :-) ) :

vb:

Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdSHow As Long) As Long

Dim ie As SHDocVw.InternetExplorerClass

ie = New SHDocVw.InternetExplorer : Application.DoEvents()



'the reliable focus:

ShowWindow(ie.HWND, 0) : ShowWindow(ie.HWND, 1)

Upvotes: 0

Michiko
Michiko

Reputation: 176

Cast the ie.Document as mshtml.HTMLDocument then set the focus.

//Like so
InternetExplorer ie = new InternetExplorer();
((mshtml.HTMLDocument)ie.Document).focus();

Upvotes: 1

Related Questions