C#.NET Compact Framework,自定义UserControl,焦点问题 [英] C# .NET Compact Framework, custom UserControl, focus issue

查看:640
本文介绍了C#.NET Compact Framework,自定义UserControl,焦点问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义的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事件KeyPreview)与foreach我检查什么控件具有焦点在面板上,并传递到下一个控件与SelectNextControl,但没有人有焦点,因为用户控件没有焦点...

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,焦点问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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