1

I have an issue with a splitter panel losing control and need to set it back.

My application uses a SplitContainer; and when my control loads data in panel2 of splitter, focus is shifting to panel1.

There are controls which are tabStop = true in both panels since tab and shift+tab needs to work on both panels and splitter.

To set it back, added a Control object in GotFocus for panel2 control and using this in _enter of panel1 to set focus back in panel2. However, I need to reset this control object; which I cannot do in _leave of panel2 since on setting back control GotFocus will be called again.

Megha
  • 21
  • 4

2 Answers2

1

I think you should implement the states management of these behaviour to synchronize control's events. For examplee, you might use some flags related to the states of your control objects and then verufy their values with some test in your GotFocus events...



update after some clearing in the comments..

On Msdn about control's focus related events, you find that the "Enter" event is always the first in the chain of events, the second is always "GotFocus". See: https://msdn.microsoft.com/it-it/library/system.windows.forms.control.gotfocus(v=vs.110).aspx

You can evaluate the control focus state variable in the "Enter" event and assign it in the "GotFocus" event avoiding to reset eanything in your leave events.

In this way you should always know the previous control focused in the "Enter" handler of each controls (you may use the same handler for all controls) and make your requirements..

If you have the need of manage some splitted controls in several panels you may also implement a dictionary that maps each panel the control focused, so if you switch from one panel to another you may always know the last control of that panel that had the focus.

Ciro Corvino
  • 2,038
  • 5
  • 20
  • 33
  • I tried setting a flag as true in _gotfocus and as false in _Leave of control A, but I need this value as true at the _enter of control B. This is so that the application can set the focus back to A if previous control was A. But I cannot let that value remain as false since B should set focus using tab and shift+tab. Then I tried setting it as false in Leave of control B but since I am doing a .Focus() to control A this flag gets reset everytime as true. – Megha Jun 22 '16 at 05:56
  • 1
    I did go through and try solutions provided in http://stackoverflow.com/questions/4428100/find-out-the-control-with-last-focus/37930272#37930272 and http://stackoverflow.com/questions/4408751/previous-focused-control-in-winforms – Megha Jun 22 '16 at 09:14
  • i had misunderstood what you meant.. I think that a module level scope control variable is a good solution for your problem – Ciro Corvino Jun 22 '16 at 12:03
  • Could you elaborate on that please? How can I set the previous focused control selectively with that? Variable I used was at project level so I can set it in gotfocus() of control A, use in enter() of control B and then reset in leave() of control A. But that was setting my variable back in gotfous() after leave(). – Megha Jun 22 '16 at 13:44
  • I was referring to the links of the solutions that you said in the comment that I upvoted.. however, however, seeing better your problem it seems to me to understand that you want to switch from a control to another by tabbing and back by shift tabbing, and you need to conserve the information about which control was the last focused.. I reply in the upon answer – Ciro Corvino Jun 22 '16 at 16:12
1

I tried multiple solutions and the safest option seemed to turn off tab control on panel1 controls whenever controls in panel2 gains focus and turn it back on when it leaves focus. This ensures application focus doesn't shift to panel1. This will also ensure that my controls are up for tabbing and shift_tabbing. Setting focus forcibly back to control in panel2 triggers gotfocus() multiple times which was defecating the purpose

Megha
  • 21
  • 4