2009-10-02 36 views
0

給定一個具有子控件的控件。你如何將焦點集中到沒有循環遍歷所有控件的tabindex最低的子控件?如何選擇tabindex最低的控件?

+0

我想不出任何其他方式編程方式確定與最低tabindex控件,除非您存儲該信息的某處創建控件時。沒有循環,你會怎麼檢查一切? – 2009-10-02 00:46:35

回答

1

此解決方案沒有您要找的性能,但它是「簡單的方法」。可能有一種我不知道的「更簡單的方法」。

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; 
    } 
} 
+0

正如我所說,你仍然需要遍歷所有的控件... – 2009-10-02 00:41:51

+1

在這種情況下,我不需要遞歸搜索。這將工作。 Dim xcon =(From Me.Controls.Cast(Of Control)()where item.TabStop = True Order By item.TabIndex Select item).First – devSpeed 2009-10-02 02:33:05

1

我會通過循環控制來做到這一點,沒有什麼錯。這可能是最簡單的解決方案。