首页 文章

使用Foreach循环在tabpages中的groupbox中检索文本框文本[复制]

提问于
浏览
0

可能重复:使用foreach循环检索GroupBox中的TextBox

我有一个标签控件,这些控件有10个标签页,每个页面有10个组框,每个组框有10个文本框,如何使用foreach循环获取所有文本框文本

2 回答

  • 0

    使用这样的东西:

    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);
                        }
                    }
                }
            }
        }
    
  • 1

    你可以使用这样的东西:

    助手功能:

    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类型的控件,如果有什么不清楚的地方请告诉我 .

相关问题