简体   繁体   中英

Setting focus to previous control

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM