给定一个具有子控件的控件。如何在不遍历所有控件的情况下将焦点放在tabindex最低的子控件上?
发布于 2009-10-02 00:32:02
这个解决方案没有您想要的性能,但它是“简单的方法”。也许有一种我不知道的“更简单的方法”。
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;
}
}发布于 2009-10-02 00:29:46
我将通过循环遍历控件来完成此操作,这没有什么错。这可能是最简单的解决方案。
https://stackoverflow.com/questions/1507190
复制相似问题