[英]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 );
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.