Welcome Guest Search | Active Topics | Sign In | Register

WebView.SetFocus() is working in Windows Form but not ASP.NET Options
James
Posted: Sunday, October 20, 2019 2:24:12 PM
Rank: Newbie
Groups: Member

Joined: 10/12/2019
Posts: 4
Hi There,

We found out that WebView's SetFocus() is working in Windows Form but not ASP.NET.

What we've tested:
1. Load google.com.
2. Focus the Search Text Box.
3. Press Tab 2 times to focus the Google Search button (a blue color border will be displayed on the button).
4. Capture the screen to confirm the button is focused.

The code below is working on Windows Form (webView1 is the WebView created automatically after added a WebControl to WinForm).

private void MyForm_Load(object sender, EventArgs e)
{
//Load page and wait for 5 seconds
webView1.LoadUrlAndWait("http://www.google.com");
EO.WebBrowser.WebView.DoEvents(5000);

//Set focus
webView1.SetFocus();
EO.WebBrowser.WebView.DoEvents(1000);

//Set focus on Search Text Box
webView1.EvalScript("document.getElementsByTagName('input')[3].focus();");
EO.WebBrowser.WebView.DoEvents(1000);

//Press Tab
webView1.SendKeyEvent(true, EO.WebBrowser.KeyCode.Tab);
webView1.SendKeyEvent(false, EO.WebBrowser.KeyCode.Tab);
EO.WebBrowser.WebView.DoEvents(1000);

//Press Tab
webView1.SendKeyEvent(true, EO.WebBrowser.KeyCode.Tab);
webView1.SendKeyEvent(false, EO.WebBrowser.KeyCode.Tab);
EO.WebBrowser.WebView.DoEvents(1000);

//Now the Google Search button is focus, capture the page to confirm the button is focused with blue border
webView1.Capture(System.IO.Path.Combine(System.IO.Path.GetDirectoryName(Application.ExecutablePath), "Test.jpg"));
}


Same code but the SetFocus() is not working in ASP.NET (We capture the screen and the Search Button is not with blue border):

protected void Page_Load(object sender, EventArgs e)
{
EO.WebBrowser.ThreadRunner threadRunner_ = new EO.WebBrowser.ThreadRunner();
webView1 = threadRunner_.CreateWebView();

threadRunner_.Send(() =>
{
//Load page and wait for 5 seconds
webView1.LoadUrlAndWait("http://www.google.com");
EO.WebBrowser.WebView.DoEvents(5000);

//Set focus
webView1.SetFocus();
EO.WebBrowser.WebView.DoEvents(1000);

//Set focus on Search Text Box
webView1.EvalScript("document.getElementsByTagName('input')[3].focus();");
EO.WebBrowser.WebView.DoEvents(1000);

//Press Tab
webView1.SendKeyEvent(true, EO.WebBrowser.KeyCode.Tab);
webView1.SendKeyEvent(false, EO.WebBrowser.KeyCode.Tab);
EO.WebBrowser.WebView.DoEvents(1000);

//Press Tab
webView1.SendKeyEvent(true, EO.WebBrowser.KeyCode.Tab);
webView1.SendKeyEvent(false, EO.WebBrowser.KeyCode.Tab);
EO.WebBrowser.WebView.DoEvents(1000);

//Now the Google Search button is focus, capture the page to confirm the button is focused with blue border
webView1.Capture(System.IO.Path.Combine(Server.MapPath("~"), "Test.jpg"));
});
}

We must set the focus on WebView in ASP.NET as our targeted page need to be focused in order to trigger some JavaScript/changes in element's class.

Please help. Thank you.
eo_support
Posted: Monday, October 21, 2019 1:16:38 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,218
Hi,

There is no reliable way for you to do this. The root of the issue is not in EO.WebBrowser ---- the root of the issue is Windows restricts which window can be active and receive focus. The focus is closely associated to foreground window and you can not make any window a foreground window. You can get an idea of such restrictions from this page:

https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setforegroundwindow

Such restriction would not be an issue if you just run the code in a GUI application because your GUI application/thread already have the active/foreground window. However in the case of an ASP.NET application, IIS process and the worker thread would not be attached to a foreground window, which makes it impossible for you to set focus to any of the window created/owned by the thread.

There might be "hacky" ways for you to manipulate foreground window to get around these rules, such as to inject code into the current foreground window process to switch it to your thread (because only the current foreground process can give the foreground status to you, you can't just take it), set focus, and then restore it to the previous foreground window, but this would be extremely unreliable as Microsoft constantly changes/optimizes the internal work/rules regarding focus because this is an extremely important part of the Windows user experience. So we would highly recommend you to change your code not to rely on focus.

Thanks!


James
Posted: Monday, October 21, 2019 8:05:55 PM
Rank: Newbie
Groups: Member

Joined: 10/12/2019
Posts: 4
Hi There,

Thank you for the reply. We tested in Windows Application, the WebView's SetFocus is working even the WinForm is not in foreground, minimized or invisible.

In this case, is it possible to run/simulate a Windows Application in ASP.NET? We'are not familiar with Windows Application and tried to run a Windows Application like this in ASP.NET but the WebPage become an infinity loop and did not stop.

1.Create a WinForm and WebControl
2.Assign the WebView to WebControl
3. Add the WebControl to WinForm
4. Run the Window Application


Below is the code in ASP.NET's WebForm's PageLoad event:

protected void Page_Load(object sender, EventArgs e)
{
EO.WebBrowser.ThreadRunner ThreadRunner = new EO.WebBrowser.ThreadRunner();
webView1 = ThreadRunner.CreateWebView();

System.Windows.Forms.Form form_ = new System.Windows.Forms.Form();
EO.WinForm.WebControl webControl_ = new EO.WinForm.WebControl();
webControl_.WebView = webView1;
form_.Controls.Add(webControl_);

ThreadRunner.Send(() =>
{
System.Windows.Forms.Application.EnableVisualStyles();
System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(false);
System.Windows.Forms.Application.Run(form_);

//Load page and wait for 5 seconds
webView1.LoadUrlAndWait("http://www.google.com");
}
}
eo_support
Posted: Tuesday, October 22, 2019 11:36:15 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,218
Hi,

You can try to run a Windows Form application without using the ThreadRunner and see if it works for you. You would simply create a background thread and then run everything inside that thread. This would include both creating the Form and calling Application.Run (you can not do them in different threads). Note that this is just a suggestion --- we won't be able to provide any further support on this if you run into any problems as this is really stretching beyond what it is designed to do.

Thanks!
James
Posted: Wednesday, October 23, 2019 1:41:57 AM
Rank: Newbie
Groups: Member

Joined: 10/12/2019
Posts: 4
Hi There,

Thank you for the suggestion. We'll try it out.


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.