2

I have a windows application with a tabcontrol. One of the tab of the tabcontrol has a webbrowser control.Now the issue that I am facing is when the focus is inside the webbrowser control, the normal Ctrl+Tab functionality of the tabcontrol is not working.I want the Ctrl+Tab to change the selected tab of tabcontrol even when the focus is inside webbrowser control in selected tab.How to achieve this ?

I have already tries overriding ProcessCmdKey.but it does not get hit when focus is inside webbrowser control.

I also tried registerhotkey method ,it works but it locks the Ctrl+Tab hotkey within my application & system doesn't respond to any other Ctrl+Tab presses outside my application when application is running, which is expected behaviour of registerhotkey.

Mironline
  • 2,755
  • 7
  • 35
  • 61
user435293
  • 53
  • 5
  • If you want to disable all webbrowser shortcuts (control+N, backspace, etc) you can set webBrowser.WebBrowserShortcutsEnabled – Sheng Jiang 蒋晟 Oct 01 '12 at 23:08
  • thanks Sheng.but I want the shortcuts in web browser to remain active.i solved the problem by using hotkey for CTRl+TAB.I unregister the hotkey when other window calls CTRL+TAB & registers again on activated event.This may not be an ideal approach but working for now. – user435293 Oct 02 '12 at 07:38

1 Answers1

0

Here is the code you need:

    If WB.ContainsFocus Then
        MsgBox("We have focus, disappearing...")
        WB.Document.Body.RemoveFocus()
    End If

Now, the question is, when to run that code. If you put it in the WebBrowser1_GotFocus event, you'll need to turn it on and off. Turn the code off if the user is interacting with the WB Control, and turn it back on when they are not and when you expect to be experiencing the problem you've mentioned.

Of course, you could add another line to ensure a particular control/tab/panel etc gets focus after you remove focus from the body. Also, here are 3 other SO questions that have answers which may help you, but these will take you in directions different to the direction I've provided, probably due to the fact that the questions are not identical to yours, but are similar enough to be useful (not listed in order of preference).

Prevent WebBrowser control from stealing focus?

Webbrowser steals focus

Focusing WebBrowser control in a C# application

UPDATE: I just wanted to add, instead of the .Body.RemoveFocus() you could do this:

WB.Document.Body.Parent.RemoveFocus()

Which I prefer, since the .Document object didn't have an explicit .RemoveFocus method on the intellisense I was gettign in VS2012 RC. This is probably referring to the HTML tag (and not the .Document object) and since the html tag is the only parent to the body tag, it makes sense, and there is no "HTML" object directly available in the intellisense under object, since you can get it via other means, so it's just more convenient doing it this way.

Cheers, and let me know if you need more info on anything.

Community
  • 1
  • 1
Erx_VB.NExT.Coder
  • 4,838
  • 10
  • 56
  • 92
  • thanks for your answer ! i'll surely give it a try.for now i solved the problem by using hotkey for CTRl+TAB.I unregister the hotkey when other window calls CTRL+TAB & registers again on activated event.This may not be an ideal approach but working for now. – user435293 Oct 02 '12 at 07:35
  • @user435293 Well, give it a try when/if you have time as it'd be an elegant way to do things (let us know how it goes if you do), I answered your post because nobody else did so as long as you have a solution that's the most important thing. Cheers. – Erx_VB.NExT.Coder Oct 02 '12 at 09:00