Change color of various Panel’s dynamically

Asked

Viewed 140 times

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 beyond impractical, what I thought was something like this:

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

That would work better...

But I’m not getting it, I even thought about Macro Substitution but I still don’t know the language very well.

Thanks in advance!

2 answers

2


One way to do this is to walk the controls of the form and check if it is a Panel, if it is, you perform the action.

For Each pnl As Control In Me.Controls
   If TypeOf (pnl) Is Panel Then
     pnl.BackColor = Color.DarkOrange
   End If
Next
  • 1

    Thank you very much! I managed to treat this way!

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.