2010-11-25 71 views

回答

2
foreach (Control c in MyForm.Controls) { 
    c.BackColor = Colors.Black; 
    c.ForeColor = Colors.White; 
} 
4

您可以通過控制循環,我相信所有的控件都有Controls屬性,它是包含控件的列表。

假設功能:

public void ChangeControlsColours(Controls in_c) 
{ 

    foreach (Control c in in_c) 
    { 
     c.BackColor = Colors.Black; 
     c.ForeColor = Colors.White; 
     if (c.Controls.length >0) //I'm not 100% this line is correct, but I think you get the idea, yes? 
      ChangeControlsColours(c.Controls) 
    } 

} 
+1

+1用於提供遞歸解決方案 – 2010-11-25 16:30:49

0

這真的取決於你想要做什麼。最優雅的方式可能是您在設計時定義的鏈接應用程序設置,然後您可以在運行時進行更改。

0
private void UpdateInternalControls(Control parent) 
    { 
     UpdateControl(parent, delegate(Control control) 
           { 
            control.BackColor = Color.Turquoise; 
            control.ForeColor = Color.Yellow; 
           }); 
    } 

    private static void UpdateControl(Control c, Action<Control> action) 
    { 
     action(c); 
     foreach (Control child in c.Controls) 
     { 
      UpdateControl(child, action); 
     } 
    } 
相關問題