获取Windows窗体中的焦点组件

时间:2014-08-11 14:20:37

标签: c# winforms compact-framework .net-2.0 windows-ce

我需要使用Windows窗体和.NET Framework 2.0获取具有焦点的组件 - C#或VisualBasic

我有一个事件,在某个时刻,收到一个文本,它需要将此文本放在TextBox组件内。但它不仅仅是一个组成部分。必须是重点组成部分。我的情况是:我正在使用从硬件阅读器获取字符串的低级应用程序和硬件通信,我必须将此文本附加到焦点TextBox

_device = new Device(Device.AvailableDevices[0].DeviceName);
_leitor = new Reader(_device);
_leitorDados = new ReaderData(ReaderDataTypes.Text, ReaderDataLengths.MaximumLabel);
_leitor.Actions.Enable();
_leitor.Actions.Read(_leitorDados);
_leitor.StatusNotify += delegate
{
    if (_leitorDados.Text == String.Empty) return;
    MessageBox.Show(_leitorDados.Text);
    _leitorDados = new ReaderData(ReaderDataTypes.Text, ReaderDataLengths.MaximumLabel);
    _leitor.Actions.Read(_leitorDados);
}; 

我的文字位于_leitorDados.Text,当我收到该活动时,我需要

focusedControl.Text = _leitorDados.Text;

但我使用的是非常有限的.NET Framework版本,2.0和我没有那么多的可能性。 提前谢谢。

  • 这个问题与Stack Overflow中的其他问题不同,因为它是关于.NET Framework的确定版本,它没有我执行此操作所需的资源。
  • .NET Framework 2.0中没有this.ActiveControl。与Win-CE一起

enter image description here

1 个答案:

答案 0 :(得分:1)

您应该使用递归方法执行此操作。试试这个:

public static Control FindFocusedComponent(Control control)
{
    foreach (Control child in control.Controls)
    {
        if (child.Focused)
        {
            return child;
        }
    }

    foreach (Control child in control.Controls)
    {
        Control focused = FindFocusedComponent(child);

        if (focused != null)
        {
            return focused;
        }
    }

    return null;
}