如果我在 groupBox 中移动文本框,如何更改 c#.net 中文本框的字体颜色?

How to change the font color of a textbox in c#.net if I move the textbox in a groupBox?

如果在 groupBox 中移动文本框,如何更改 c#.net 中文本框的字体颜色? 它在没有组框时有效,但如果文本框在组框中,则字体颜色不会改变。

这是进入分组框之前有效的初始代码。

foreach (object t in this.Controls)
  if (t.GetType() == typeof(TextBox))
      ((TextBox)t).BackColor = Color.AntiqueWhite;

当您在 this.Controls 上循环时,您只是获得了该级别的控件,即控件是您的表单(我认为是)的直接子控件。

尝试:

foreach (object t in groupBox1.Controls)
        if (t.GetType() == typeof(TextBox))
            ((TextBox)t).BackColor = Color.AntiqueWhite;

如果需要查找整个窗体上的所有文本框,写一个递归函数遍历整个控件树:

private void ForAll<T>( Control c, Action<T> func ) where T : Control
{
    if( c is T )
        func( (T)c );
    foreach( Control child in c.Controls )
        ForAll( child, func );
}

并像这样使用:

ForAll<TextBox>( this, c => c.BackColor = Color.AntiqueWhite );