7

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

The problem is that when the form hosting the browser control is loaded, the focus is on the form. When the TAB key is pressed, the focus does not shift to the first hyperlink. However, if I perform a mouse click on the control and then hit the TAB key, the tab focus is now on the first hyper link. I tried using Select() on the WebBrowser control and then I called Focus(), but it doesn't solve the problem.

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

Cheers, Harish

Ashraf Bashir
  • 9,686
  • 15
  • 57
  • 82
HIyer
  • 283
  • 1
  • 4
  • 10

4 Answers4

12

I guess it might be because the focus is set before the page is fully loaded. Try this:

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 could also automatically select the focus on the first link directly by getting the HtmlElement of that first link.

If the above doesn't work, you might want to check other parts of your code to see if anything else is capturing the focus. Try searching for Select, Focus and ActiveControl in your code.

Fun Mun Pieng
  • 6,751
  • 3
  • 28
  • 30
  • This doesn't help in the current issue :( – Ashraf Bashir Mar 01 '12 at 16:20
  • If I may ask, which part doesn't help? The sample code that selects the WebBrowser control and allows the users to select the link with the TAB key? OR the part that explains you need to use the HtmlElement of the first link to set focus on it? OR the part that ask to check whether anything else is capturing the focus? Do let me know which part isn't clear. I will explain further if required. Thank you. – Fun Mun Pieng Mar 06 '12 at 09:25
  • @AshrafBashir, could you explain what the current issue is, that I failed to help? – Fun Mun Pieng Mar 06 '12 at 09:29
  • The issue that doesn't work is the sample code that selects the WebBrowser control and allows the users to select the link with the TAB key – Ashraf Bashir Mar 13 '12 at 08:26
1

This is my solution

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();
    }
}
athspk
  • 6,722
  • 7
  • 37
  • 51
Seraph
  • 11
  • 1
1

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

Ashraf Bashir
  • 9,686
  • 15
  • 57
  • 82
0

In a normal scenario it should be sufficient for you to set the TabIndex of the WebBrowser control to zero. This way, when the form loads the control will be focused and pressing TAB will iterate through the links.

Note that you should also change the TabIndex of the other controls on the form.

If this does not solve your problem, you need to add more detail on the complexity of the form hosting the control.

João Angelo
  • 56,552
  • 12
  • 145
  • 147