Search code examples
c#geckofx

How to always keep browser on focus


When I click radio button or navigate through tabs my geckofx web browser loose focus. I can get back to focus by using browser.Focus() it works well but if there is for example two tabs 1tab and 2tab. On 2tab is my webbrowser. When I go to tab1 my browser in tab2 loose focus and command browser.Focus() wont work. Its really annoying that everytime I do some stuff on my UI gecko loose focus. Is there any way how I could keep it ALWAYS on focus?

Thanks.

Edit: I thinked about making UI elements not to focus on them so browser wont loose its focus. enter link description here Found this, used that class and it works with buttons. Ill try to do this on tabs.


Solution

  • It's simple. First add this code on Form_load :

    private void Form1_Load(object sender, EventArgs e)
            {
                webBrowser1.TabIndex = 0;
                webBrowser1.Focus();
            }
    

    Then make all other controls' Tabstop property to false. And on every controls clicking event add this code : webBrowser1.Focus();

    For example I use this code for Button and RadioButton.

    private void radioButton1_Click(object sender, EventArgs e)
        {
            radioButton1.TabStop = false;
            webBrowser1.Focus();
            webBrowser1.LostFocus += webBrowser1_LostFocus;
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
            button1.TabStop = false;
            webBrowser1.Focus();
            webBrowser1.LostFocus += webBrowser1_LostFocus;
        }
    

    Do this for all other controls in your form. You also need to define webBrowser1_LostFocus void for that.

    void webBrowser1_LostFocus(object sender, EventArgs e)
        {
            webBrowser1.Focus();
        }
    

    Update : You can also make a void for focusing. Then use this void in everywhere you want. Try this :

    public void SetFocus(Control webbrowser)
    {
     // Set focus to the control. 
       if(webbrowser.CanFocus)
       {
          webbrowser.Focus();
       }
    }