Form focusing issue

I am working in Visual Studio 2008 .NET in C#, and I have 2 forms meant to be run concurrently.  There is a grid in the first form and a TabControl on the 2nd form.  When you change selected rows in the grid on the first form, it is going to cause one of the tabs on the 2nd form to become selected.  I need this to happen without the grid on the first form losing focus.

Currently, the CellEnter event of the grid in form 1 is checking to see if the row changed.  If so, it will call a method in form 2 that selects the required tab.  Here's a sample of the code:

//In form 1
private void OnRowChange() //Called by CellEnter event, only if row changed
  List<String> lstDocs = getDocIDList();
  //Form2 is reference to form 2
  //editingGrid is the DataGridView in the form

//In form 2
public void ViewLoadedDocs(List<String> pDocIDs)
  int index = getFirstDocIndex(pDocIDs);
  //docTabs is the TabControl on this form

This works most of the time and successfully restores focus to the DataGridView in form 1.  However, and seemingly randomly, Form2 will remain focused after this function is called.  Sometimes form 2 keeps focus after changing just one row in the grid, sometimes it's after changing a dozen rows.  I have tried setting the focus in various different ways, setting it from ViewLoadedDocs instead of OnRowChange(), setting focus to form 1 itself instead of the grid, and selecting the tab in ViewLoadedDocs in differenet ways (changing SelectedIndex, etc).  It will not occur if I do not select a tab in form 2, which is of course the only operation that would give form 2 focus.  

Does anyone have any idea why form 1 would sometimes not maintain focus even after the Focus() method was called?  Is the selection of the tab in form 2 happening on a separate thread somehow and sometimes executing after Focus() is called in form 1?  If so, how can I prevent that?  Is there a way to prevent form 2 from ever getting focus to begin with, but still change the selected tab in the TabControl?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

How about this idea: Subscribe to the tabControl.Selected event and in the Selected event then set the focus to the editingGrid.

You need to make sure that the tab control is all set and done before setting the focus back to the grid.

Out of curiousity, why would you have two different forms? Seems like this should all go on one form.
DigitechComputerAuthor Commented:
Thanks for the quick reply.

I had actually tried doing almost the same thing (though using the SelectedIndexChanged event instead).  I tried doing the same with the Selected event instead, but it made no difference.  

The way its set up, form 1 is a massive editing form with a grid and tons of fields, and form 2 is a document viewer.  The forms are meant to be used in a dual-monitor setup where form 1 takes up all of the first screen and form 2 takes up all of the 2nd screen.  Both forms are also often used completely independently of each other, so combing them both into one form would not be practical.
Well, I just hacked together a sample app and could not reproduce what you see. The grid form never loses focus even though I'm changing the tab page of the tab control. I tried both SelectTab and SelectedIndex members of the tab control.

I think there is some other code in your app that is interfering.  Are you doing something else as a side effect of changing the tab page? Does the tab control "take" the focus if the page changes? Have you hooked onto any other events of the grid that might be changing things? Have you tried stripping your app down to just the essentials?
Why Diversity in Tech Matters

Kesha Williams, certified professional and software developer, explores the imbalance of diversity in the world of technology -- especially when it comes to hiring women. She showcases ways she's making a difference through the Colors of STEM program.

DigitechComputerAuthor Commented:
I should have mentioned this from the start, but each page of the tab control has a WebBrowser object on it.  It looks like the WebBrowser was the culprit.  When I remove the WebBrowser and left the tab pages empty, form 1 never loses focus.

The problem now is figuring out why the WebBrowser steals focus sometimes, or how I can work around it.  I tried plugging into the WebBrowser's VisibleChanged, Validated and DocumentCompleted events and manually setting focus to form 1, but it should still occasionally steal form 1's focus.  I tried the same with the WebBrowser's GotFocus event, and that did the trick.  Of course, that prevents form 2's tab control from ever having focus.  

I can probably work around this with a boolean toggle variable that will be true whenever input occurs on the tab control though the mouse or the keyboard, but false otherwise.  I set the GotFocus event to only set focus back to form 1 when this variable is false.  That is a really kludgey solution, though.  Do you know of any better way to work around this issue with the WebBrowser class?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DigitechComputerAuthor Commented:
I was afraid that there wouldn't be any "perfect" solution to this.  The pages being loaded do sometimes have javascript, but some are just PDF documents.  Still, it's probably something similar in the Acrobat viewer causing these WebBrowsers to steal focus.  

Fortunately, my workaround seems to be working.  When the WebBrowser steals focus, it still fires the GotFocus event of the WebBrowser.  I added a boolean toggle that, when on, will always manually give Form 1 focus whenever the WebBrowser's GotFocus event is fired.  There were no good places in the code or events to toggle this boolean off again (no matter how late in the code after setting the selected tab, the WebBrowser would still sometimes manage to steal focus).  Instead, I set up a timer (blech...) that will toggle the boolean back off after 100ms whenever a tab is selected in Form 2.  Not the best solution, but Form 2 is only "unclickable" for a tenth of a second after changing records in Form 1, so it shouldn't ever affect a user.  It's not the most elegant solution, but it seems to be working every time.

Thanks again for the help!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.