我有一个自定义 UserControl(一个标签和一个文本框).
I have a custom UserControl (a label and a textbox).
我的问题是我需要处理按键按下、按键向上事件以在表单中的控件之间导航 (.NET Compact Framework 文本框、组合框等).使用 .NET Compact Framework 框架提供的控件,它可以工作,但是当我到达我编写的用户控件时,该控件没有获得焦点(里面的文本框获得焦点)所以从这个用户控件我无法导航,因为在面板中我无法控制谁拥有焦点.
My problem is I need to handle the key down, key up events to navigate between controls in the form (.NET Compact Framework textbox, combobox, etc). With the controls provided by the .NET Compact Framework framework it works, but when I reach a usercontrol written by me, that control don`t get focus (the textbox inside get the focus) so from this usercontrol I cannot navigate because in the panel I don't have any control of who have focus.
一个小模型:Form->Panel->controls-> on keydown event (使用 KeyPreview) with a foreach 我检查面板上有焦点的控件并使用 SelectNextControl 传递给下一个控件,但没有人有焦点,因为 usercontrol 没有获得焦点...
A little mock up : Form->Panel->controls -> on keydown event (using KeyPreview) with a foreach I check what control have focus on the panel and pass to the next control with SelectNextControl, but no one have focus because the usercontrol don`t got focus...
我试图处理文本框 gotFocus 事件并将焦点放在用户控件上,但我得到了一个无限循环..
I tried to handle the textbox gotFocus event and put focus to the user control, but I got an infinite loop..
有人知道我能做什么吗?
Does somebody have any idea what can I do?
我们在 Compact Framework 上做了完全相同的事情,添加了一个全局焦点管理器,支持使用键盘输入在控件之间导航.
We've done the exact same thing on Compact Framework, adding a global focus manager that supports navigating between controls using keyboard input.
基本上,您需要做的是向下递归控件树,直到找到具有焦点的控件.它的效率不是很高,但只要每个关键事件只执行一次,这应该不是问题.
Basically, what you need to do is to recurse down the tree of controls until you find a control that has focus. It's not terribly efficient, but as long as you only do it once per key event, it shouldn't be an issue.
为我们的递归焦点查找函数添加了代码:
Added the code for our recursive focus finding function:
public static Control FindFocusedControl(Control container)
{
foreach (Control childControl in container.Controls) {
if (childControl.Focused) {
return childControl;
}
}
foreach (Control childControl in container.Controls) {
Control maybeFocusedControl = FindFocusedControl(childControl);
if (maybeFocusedControl != null) {
return maybeFocusedControl;
}
}
return null; // Couldn't find any, darn!
}
这篇关于C# .NET Compact Framework,自定义 UserControl,焦点问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!