2010-01-08 8 views
0

Howto atteindre les commandes dans un tabcontrol, je veux colorer toutes les textboxen en couleur avec une méthode simple foreach:Fetch contrôles dans un tabcontrol

  foreach (Control c in this.Controls) 
      { 
//btw I get the next error at this line: System.Windows.Forms.TabControl' is a 'type', which is not valid in the given context 
       if (c == System.Windows.Forms.TabControl) 
       { 
       c.BackColor = Color.FromArgb(240, 240, 240); 
       } 
      } 

      for (int i = 0; i < this.Controls.Count; i++) 
      { 
       if (this.Controls[i].GetType().ToString() == "System.Windows.Forms.Textbox") 
       { 
       this.Controls[i].BackColor = Color.FromArgb(240, 240, 240); 
       } 
      } 

Quelqu'un pourrait-il aider mon changement d'un des deux codes

+0

S'il vous plaît marquer vos questions correctement – skaffman

Répondre

1

Vous aurez besoin de naviguer un peu plus de contrôle nesti ng et les opérateurs que vous recherchez (pour éliminer l'erreur) sont les is et as opérateurs:

est: http://msdn.microsoft.com/en-us/library/scekt9xw(VS.71).aspx

comme: http://msdn.microsoft.com/en-us/library/cscsdfbt(VS.71).aspx

foreach (Control c in this.Controls) 
{ 
    TabControl tabControl = c as TabControl; 
    if (tabControl != null) 
    { 
     foreach (TabPage page in tabControl.TabPages) 
     { 
      foreach (Control innerControl in page.Controls) 
      { 
       if (innerControl is TextBox) 
       { 
        innerControl.BackColor = Color.FromArgb(240, 240, 240); 
       } 
      }       
     } 
    } 
} 
0
  if (c == System.Windows.Forms.TabControl) 
      { 
      c.BackColor = Color.FromArgb(240, 240, 240); 
      } 

pourrait se faire comme

TabControl tc = c as TabControl; 
if(tc != null) 
{ 
    tc.BackColor = Color.FromArgb(240, 240, 240); 
}