可能重複:
Using a foreach loop to retrieve TextBox's within a GroupBox使用foreach循環來檢索的TabPages
我有一個標籤控制和這些控制有10個標籤頁,每頁有10個groupboxes每個groupbox有10個文本框,我如何獲得使用foreach循環的所有文本框文本
可能重複:
Using a foreach loop to retrieve TextBox's within a GroupBox使用foreach循環來檢索的TabPages
我有一個標籤控制和這些控制有10個標籤頁,每頁有10個groupboxes每個groupbox有10個文本框,我如何獲得使用foreach循環的所有文本框文本
你可以使用像這樣的東西:
助手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,請讓我知道如果有什麼不清楚。
使用這樣的事情:
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);
}
}
}
}
}