Focusing WebBrowser controls in a C # application

I have a WebBrowser hosted in Form windows. The control is used to display hyperlinks that are created at runtime. These links point to some HTML pages and PDF documents.

The problem is that when the layout of the browser control is loaded, the focus is on the form. When the TAB key is pressed, the focus does not go to the first hyperlink. However, if I click on the mouse button on the control and then press the TAB key, the focus of the tab is now on the first hyperlink. I tried using Select() in a WebBrowser , and then I called Focus() , but this does not solve the problem.

Any ideas on how to set the tab to the first hyperlink at startup? Thanks.

Cheers, Harish

+6
c #
source share
4 answers

I think this may be because the focus is set before the page is fully loaded. Try the following:

 private void Go(string url) { webBrowser1.Navigate(url); webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted); } void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { webBrowser1.Document.Body.Focus(); } 

You can also automatically select focus on the first link directly by receiving the HtmlElement that first link.

If the above does not work, you can check other parts of your code to see if something else captures the focus. Try searching for Select , Focus and ActiveControl in your code.

+10
source share

Use form.ShowDialog(form) instead on form.Show() , then it will work!
where form is the executable instance of your form windows

+1
source share

This is my decision

 private void txtAdres_KeyPress(object sender, KeyPressEventArgs e) { int licznik = 1; if (e.KeyChar == (char)13) { string adres = txtAdres.Text; webBrowser1.Navigate(adres); licznik = 0; } if (licznik == 0) { webBrowser1.Focus(); } } 
+1
source share

In a normal scenario, it should be enough for you to set the TabIndex of the WebBrowser to zero . Thus, when loading the form, the control will be focused and pressing TAB will go through the links.

Note that you must also change the TabIndex other controls in the form.

If this does not solve your problem, you need to add more detailed information about the complexity of the form in which the control is located.

0
source share

All Articles