【问题标题】:Get the focused component in Windows Forms获取 Windows 窗体中的焦点组件
【发布时间】:2014-08-11 14:20:37
【问题描述】:

我需要使用 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 一起使用

【问题讨论】:

  • 我想没有.net 2.5。还使用哪个版本的框架和 Windows CE 标记您的问题
  • 是的。我搞砸了。是.NET 2.0。我现在在属性中检查了它。谢谢。
  • 根据documentation 支持。支持平台有Windows CE
  • 让我截图...

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


【解决方案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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-08
    • 2014-11-22
    • 1970-01-01
    • 2010-09-11
    • 1970-01-01
    • 1970-01-01
    • 2012-06-07
    相关资源
    最近更新 更多