?
Solved

Form focusing issue

Posted on 2009-12-23
6
Medium Priority
?
673 Views
Last Modified: 2013-12-17
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
  Form2.ViewLoadedDocs(lstDocs);
  //editingGrid is the DataGridView in the form
  this.editingGrid.Focus();
}

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


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?
0
Comment
Question by:DigitechComputer
  • 3
  • 3
6 Comments
 
LVL 8

Expert Comment

by:dericstone
ID: 26114915
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.
0
 

Author Comment

by:DigitechComputer
ID: 26114998
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.
0
 
LVL 8

Expert Comment

by:dericstone
ID: 26118425
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?
0
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 

Author Comment

by:DigitechComputer
ID: 26131440
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?
0
 
LVL 8

Accepted Solution

by:
dericstone earned 2000 total points
ID: 26143768
0
 

Author Comment

by:DigitechComputer
ID: 26145300
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!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month17 days, 10 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question