2011-11-22 62 views

回答

0

你可以使用像這樣的東西:

助手FUNC:

public static IEnumerable<T> PrefixTreeToList<T>(this T root, Func<T, IEnumerable<T>> getChildrenFunc) { 
    if (root == null) yeild break; 
    if (getChildrenFunc == null) { 
     throw new ArgumentNullException("getChildrenFunc"); 
    } 
    yield return root; 
    IEnumerable children = getChildrenFunc(root); 
    if (children == null) yeild break; 
    foreach (var item in children) { 
     foreach (var subitem in PrefixTreeToList(item, getChildrenFunc)) { 
       yield return subitem; 
     } 
    } 
} 

用法:

foreach (TextBox tb in this.PrefixTreeToList(x => x.Controls).OfType<TextBox>()) { 
    //Do something with tb.Text; 
} 

我想,我需要解釋一下,我在這裏做什麼,這個代碼遍歷的控制滿樹,選擇那些在他們的,其中有類型的TextBox,請讓我知道如果有什麼不清楚。

1

使用這樣的事情:

foreach (TabPage t in tabControl1.TabPages) 
    { 
     foreach (Control c in t.Controls) 
     { 
      if (c is GroupBox) 
      { 
       foreach (Control cc in c.Controls) 
       { 
        if (cc is TextBox) 
        { 
         MessageBox.Show(cc.Name); 
        } 
       } 
      } 
     } 
    }