如何获得目前专注的控制? [英] How to get the currently focused control?

查看:94
本文介绍了如何获得目前专注的控制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我打电话给其他表格,那么当前的表格控件将失去焦点。



所以我必须将焦点重新放回原始控件。



但我不知道以前专注的控制。



如何获得当前专注的控件?

If I call other forms then the current form control loses focus.

So I'll have to give the focus back to the original control.

But I don't know the previously focused control.

How to get the currently focused control?

推荐答案

每个 ContainerControl (包括表单)都有 ActiveControl 属性。您可以使用它来获取当前活动的控件。



您可能需要挂钩离开 LostFocus 您感兴趣的任何控件的事件,并将最后一个活动控件保存在局部变量中,以便能够恢复到之前的控件。
Every ContainerControl (including Form) has an ActiveControl property. You can use this to get the currently active control.

You may have to hook into either the Leave or LostFocus events of any controls you're interested in and save the last active control in a local variable to be able to revert to the previous control.

使用标签属性(所有控件都有此属性包括表单)。您可以将当前聚焦的控件名称存储在Tag属性中。这样的东西。



Make use of the tag property(All control's have this property including the form) .You can store the currently focused controls name in the Tag property.Something like this.

btnCall.Tag = txtNotes;





当您返回表格时,请检查对于您已设置的标签属性&重新关注该控件。



When you are back to the form,check for tag property which you have set & bring back the focus on that control.


.Net Framework似乎没有任何功能来执行此操作,但我找到了这篇文章 [ ^ ]提供了实现Win32 API函数的代码。有趣的阅​​读......
The .Net Framework doesn't appear to have any function to do this, but I found this article[^] which provides code to implement a Win32 API function to do so. Interesting reading...


这篇关于如何获得目前专注的控制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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