简体   繁体   English

将焦点设置到上一个控件

[英]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; 我的应用程序使用SplitContainer; and when my control loads data in panel2 of splitter, focus is shifting to panel1. 当我的控件将数据加载到拆分器的panel2中时,焦点转移到了panel1上。

There are controls which are tabStop = true in both panels since tab and shift+tab needs to work on both panels and splitter. 在两个面板中都有tabStop = true的控件,因为tab和shift + tab都需要在面板和拆分器上都起作用。

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. 要重新设置它,请在GotFocus中添加一个Panel2控制控件对象,并在panel1的_enter中使用此对象将焦点重新设置在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. 我无法在panel2的_leave中执行此操作,因为在设置后退控件时,将再次调用GotFocus。

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... 例如,您可以使用一些与控件对象的状态相关的标志,然后在GotFocus事件中通过一些测试验证它们的值...



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". 在有关控件的焦点相关事件的Msdn上,您发现“ Enter”事件始终是事件链中的第一个事件,第二个始终是“ GotFocus”。 See: https://msdn.microsoft.com/it-it/library/system.windows.forms.control.gotfocus(v=vs.110).aspx 请参阅: 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. 您可以在“ Enter”事件中评估控件焦点状态变量,并在“ GotFocus”事件中对其进行分配,从而避免在请假事件中重置任何内容。

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. 我尝试了多种解决方案,并且最安全的选项似乎是在panel2中的控件获得焦点时关闭panel1控件上的选项卡控件,并在它离开焦点时重新打开它。 This ensures application focus doesn't shift to panel1. 这样可以确保应用程序重点不会转移到panel1上。 This will also ensure that my controls are up for tabbing and shift_tabbing. 这还将确保我的控件可以进行制表和shift_tabbing操作。 Setting focus forcibly back to control in panel2 triggers gotfocus() multiple times which was defecating the purpose 强制将焦点设置回panel2中的控件会多次触发gotfocus(),这超出了目的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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