简体   繁体   English

“活动”控件和“焦点”控件之间的区别

[英]Difference between “Active” control and “Focused” control

I'm trying to figure out why these two lines of code sometimes return different values: 我试图弄清楚为什么这两行代码有时返回不同的值:

var focus = FindFocusedControl(_targetForm).Name;
var active = _targetForm.ActiveControl.Name;

FindFocusedControl comes from here : FindFocusedControl来自这里

private static Control FindFocusedControl(Control control)
{
    var container = control as ContainerControl;
    while (container != null)
    {
        control = container.ActiveControl;
        container = control as ContainerControl;
    }
    return control;
}

Is my active simply the one least able to be drilled down into? 我的active是最不容易被挖掘的人吗? Does it depend on whether the control that actually has focus is inside a UserControl ? 它是否取决于实际上具有焦点的UserControl是否在UserControl内部?

_targetForm.ActiveControl will return the control on the form that either has the focus or one of its child controls has the focus. _targetForm.ActiveControl将在具有焦点或其子控件之一具有焦点的窗体上返回控件。

Your FindFocusedControl implementation does the same, but recursively checks the child controls until it reaches the control that has focus, and returns that. 您的FindFocusedControl实现执行相同的操作,但是递归检查子控件,直到子控件到达具有焦点的控件,然后将其返回。

So for controls directly on the form, they will return the same value, but for controls stacked on panels or other containers, the returned values will be different. 因此,对于直接在窗体上的控件,它们将返回相同的值,但是对于堆叠在面板或其他容器上的控件,返回的值将不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM