Change color of several Panel's dynamically

1

I have the following code in the button action:

 pnl1.BackColor = Color.DarkOrange
 pnl2.BackColor = Color.DarkOrange
 pnl3.BackColor = Color.DarkOrange
 pnl4.BackColor = Color.DarkOrange
 pnl5.BackColor = Color.DarkOrange
 pnl6.BackColor = Color.DarkOrange
 ... tenho 100 Panel'seguinte

Repeating the 100x code becomes ridiculous as well as unfeasible, which I thought was something like this:

For i = 0 to 100    
     (pnl+i.toString).BackColor = Color.DarkOrange
Next

This would work better ...

But I'm not getting it, I even thought about Macro Replacement but I still do not know the language very well.

Thanks in advance!

    
asked by anonymous 10.03.2015 / 04:33

2 answers

2

One way to do this is to go through the controls the form and verify that it is Panel , if it is, you execute the action.

For Each pnl As Control In Me.Controls
   If TypeOf (pnl) Is Panel Then
     pnl.BackColor = Color.DarkOrange
   End If
Next
    
10.03.2015 / 04:59
1

A very simple way:

For Each pI As Panel In MyClass.Controls
    pI.BackColor = Color.DarkOrange
Next

If you want for a group of panels:

Dim PaneisParaEditar As Panel() = { Pane1, Panel2, Panel3 }
For Each pI As Panel In PaneisParaEditar
    pI.BackColor = Color.DarkOrange
Next
    
02.05.2015 / 06:50