C# 如何访问组框中的文本框?

C# 如何访问组框中的文本框?,c#,winforms,hierarchical,C#,Winforms,Hierarchical,我设计了一个windows窗体,它将textbox1直接放在窗体上,将textbox2放在goupbox1上。在代码下方运行只会更改textbox1的文本。我在谷歌上搜索了很多,但都找不到解决办法。如何访问textbox2 public Form1() { InitializeComponent(); foreach (TextBox txtBox in this.Controls.OfType<TextBox>()) { txtBox.Ent

我设计了一个windows窗体,它将
textbox1
直接放在窗体上,将
textbox2
放在
goupbox1
上。在代码下方运行只会更改
textbox1
的文本。我在谷歌上搜索了很多,但都找不到解决办法。如何访问
textbox2

public Form1()
{
    InitializeComponent();
    foreach (TextBox txtBox in this.Controls.OfType<TextBox>())
    {
        txtBox.Enter += textBox_Enter;
        txtBox.Text = "123"; //To test if the text box is recognized or not
    }
}
public Form1()
{
初始化组件();
foreach(此.Controls.OfType()中的TextBox-txtBox)
{
txtBox.Enter+=textBox\u Enter;
txtBox.Text=“123”//测试文本框是否被识别
}
}
您可以使用递归。
我建议您使用以下扩展方法:

public static IEnumerable<T> GetAllControls<T>(Control control)
{
    var controls = control.Controls.OfType<T>();
    return control.Controls.Cast<Control>()
        .Aggregate(controls, (current, c) => current.Concat(GetAllControls<T>(c)));
}
公共静态IEnumerable GetAllControls(控件)
{
var controls=control.controls.OfType();
返回控件.Controls.Cast()
.Aggregate(controls,(current,c)=>current.Concat(GetAllControls(c));
}
用法:

var textBoxes = GetAllControls<TextBox>(this);
var textboxs=GetAllControls(此);

亲爱的Dmitry,你的回答正是我需要的。但是没有足够的解释。您是否有可能进一步解释您的解决方案?具体不清楚的是什么?请问一个具体的问题,你能给我提供一些关键字来谷歌你的解决方法吗?