Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Winforms 如何选择索引最低的控件?_Winforms - Fatal编程技术网

Winforms 如何选择索引最低的控件?

Winforms 如何选择索引最低的控件?,winforms,Winforms,给定一个具有子控件的控件。如何在不遍历所有控件的情况下,将焦点交给tabindex最低的子控件?我将遍历这些控件,这没有什么错。这可能是最简单的解决方案。此解决方案没有您想要的性能,但它是“简单的方法”。可能有一种我不知道的“更简单的方法” var firstControl = this.AllChildControls().OrderBy(m => m.TabIndex).First(); firstControl.Focus(); 代码段依赖于以下扩展方法 /// <summa

给定一个具有子控件的控件。如何在不遍历所有控件的情况下,将焦点交给tabindex最低的子控件?

我将遍历这些控件,这没有什么错。这可能是最简单的解决方案。

此解决方案没有您想要的性能,但它是“简单的方法”。可能有一种我不知道的“更简单的方法”

var firstControl = this.AllChildControls().OrderBy(m => m.TabIndex).First();
firstControl.Focus();
代码段依赖于以下扩展方法

/// <summary>
/// Preforms a preorder iteration through all children of this control, recursively.
/// </summary>
/// <returns></returns>
public static IEnumerable<Control> AllChildControls(this Control control)
{
   foreach (Control child in control.Controls)
   {
        yield return child;
        foreach (var grandchild in child.AllChildControls())
            yield return grandchild;
    }
}
//
///通过该控件的所有子控件递归地执行预排序迭代。
/// 
/// 
公共静态IEnumerable AllChildControls(此控件)
{
foreach(Control.Controls中的控件子级)
{
退换子女;
foreach(child.AllChildControls()中的var-granter)
收益回报孙子;
}
}

除非在创建控件时将该信息存储在某个位置,否则我想不出任何其他方法以编程方式确定tabindex最低的控件。如果没有循环,你怎么检查所有东西呢?在这种情况下,我不需要递归搜索。这会奏效的。Dim xcon=(来自Me.Controls.Cast(控件)中的项(),其中item.TabStop=True Order By item.TabIndex Select item)。首先