表格没有得到适当的关注 [英] form not getting proper focus

查看:46
本文介绍了表格没有得到适当的关注的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我已经写了一个小表格的应用程序.我已通过使用低级键盘挂钩启用了快捷键,以便在需要时将焦点放在应用程序上.
一切正常,我按下快捷键,即应用程序第一次获得适当控制时,即LControlKey + LControlKey.
但是,当我停用并重做快捷键时,任务栏上的应用程序图标开始闪烁,并且应用程序中的文本框似乎获得了焦点(光标闪烁),但仍未到达控件.我之所以这样说,是因为当我尝试编写某些东西时,应用程序上什么也没写.

我通过使用也尝试过的form.Activate()将焦点集中在应用程序上.可见,但是没有运气.


i have written a small form application. I have enabled shortcut key by using low level keyboard hook to give focus to application when needed.
All is working fine, I press the short key i.e. LControlKey+LControlKey for the first time the application get proper control.
But when I deactivate and redo the shortcut key, the icon of the application on the taskbar starts blinking and textbox in the application seems to get the focus(cursor blinking) but the control is still not reached. I say this because when i try to write something nothing gets written on the application.

I am giving focus to the application by using form.Activate() I have also tried from.Visible but with no luck.
Does any have any idea why is this happening?

推荐答案

您说的听起来有些怪异,但是是的,您可以搞乱某些事情的聚焦行为.不幸的是,您没有完整描述您的行为.我知道,这不太容易.

您可以执行以下操作:您应该创建一个简短的可能的代码示例来说明问题.如果最好放在一个文件中(您可以通过避免使用Designer并将所有内容都写在与入口点相同的文件中(Main方法)来做到这一点.两个短文件也是可以接受的,但请使其简短但完整详细描述步骤;单击并单击要使用的内容;使用上面的改进问题"将其发布.很有可能,您将自己解决问题,因为仅准备一个简短的代码样本就可以解决一个问题,这通常会有所帮助拿东西.在这种情况下,除了发表评论现在就可以使用",不要发布任何内容.好吗?

同时,让我解释一下聚焦的本质. 焦点"是指键盘焦点",仅此而已.在任何给定的时刻,只专注于整个系统.您可以集中控制调用System.Windows.Forms.Control.Focus的控件.没有诸如散焦"之类的操作,因为这与先前的陈述是不合逻辑且矛盾的.因此,控件只有在关注其他内容时才能失去关注.

现在,为简单起见,让我们只考虑始终具有至少一个启用控件的表单.方法Form.Activate不会直接聚焦任何内容,而是会激活表格.如果在某个控件集中的状态下停用了该窗体,则该窗体会记住该最后一个焦点"控件.如果再次激活该表单,则会记住焦点;相应的控件将自动专注于激活.

原则上,您可以在停用表单之前创建病理情况.例如,禁用焦点控件,将其隐藏或删除.可能是另外一回事.在这种情况下,重点放在哪里–我不知道,这取决于因素的数量.老实说,我无法想象所有可能的情况.我承认,重点关注的情况可能足够复杂,因此如果没有代码示例,我将无法修复它.从自己尝试开始.

—SA
What you say sounds a bit weird, but yes, one can screw up the focusing behavior doing certain things. Unfortunately, you did not describe you behavior in full. I understand, this is not too easy.

Here is what you can do: you should create a shorted possible code sample manifesting the problem. If better be in one file (you can do it by avoiding using Designer and writing everything in code in the same file as your entry point (Main method). Two short files would be acceptable, too, but make it short but complete. Describe in detail the steps; what to click and hit. Post it using "Improve question" above. Chances are, you will fix the problem by yourself, because just the fact you prepare a short code sample focusing only on one problem often helps to get things. In this case, don''t post anything except your comment "it works now". OK?

In the meanwhile, let me explain it the essence of the focusing. The "focus" means "keyboard focus" and nothing else. At any given moment of time, there is only on focus on the whole system. You can focus a control calling System.Windows.Forms.Control.Focus. There is not such operation as "defocus" as it would be illogical and contradictory to the previous statement. Therefore, the control can only loos focus if something else is focused.

Now, for simplicity, let''s consider only the forms which have at least one enabled control at all times. The method Form.Activate does not directly focus anything, it activates the form. If the form was deactivated with some control focused, this "last focused" control is remembered by the form. If the form is activated again, the focus is remembered; and the appropriate control is automatically focused upon activation.

In principle, you could create pathological situation before the form was deactivated; for example, disable a focused control, hide it or delete it. It could be something else. Where is the focus in this case — I don''t know, it depends on the number of factors. Honestly, I cannot imagine all possible situations; I admit the situation with focus could be complex enough, so I would not be able to fix it without a code sample. Start with trying by yourself.

—SA


这篇关于表格没有得到适当的关注的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆