Controls looping

LamKat

Newcomer
Joined
Dec 3, 2011
Hey people

i've been having problems with vb.net.
i like to use 'controls' with looping as to get rid of the need to do basically the same command over and over again. but this comand doesn't work for all objects... and i would like some help

so the code goes a little like this
Code:
        For Counter As Integer = 1 To 64 Step +1
            Controls("Panel" & Counter).BackColor = Color.Black
        Next

as you could guess this is for a game of chess
 

LamKat

Newcomer
Joined
Dec 3, 2011
I've just had a look at the code set in a new form. and i've isolated the problem to the fact that i've been using a tablelayoutpanel with the panels inside the table.
at which point it doesn't work...
though it also doesn't work for Lineshapes without a table

again can anyone think of a solution that doesn't involve not using a tableLayoutPanel
 

PlausiblyDamp

Administrator
Joined
Sep 4, 2002
Location
Lancashire, UK
If the controls in question are inside of another container then you would need to loop over the container's control collection rather than the form's control collection.

If these controls are nested inside of multiple containers then you might find it easier to write a routine that searches recursively through the controls.
 

LamKat

Newcomer
Joined
Dec 3, 2011
PlausiblyDamp,

can you think of any, and if so could you post an example of these nested controls for me to use.

thanks, LamKat
 

snarfblam

Ultimate Contributor
Joined
Jun 10, 2003
Location
USA
LamKat, this is recursion in it's most basic form.

First, make sure you need recursion. Are the controls you need to access all contained directly within one container? Don't assume. Check. A control that hosts other controls might manage it's layout by placing the hosted controls inside child controls. For example, I believe that each half of a SplitContainer is a separate Panel control. If you add a control to the SplitContainer, it actually gets added to a Panel within the SplitContainer. Does a TableLayoutPanel do the same thing? You'll have to check. Look at the documentation, or examine it during runtime in the debugger.

If your controls are nested (placed in sub-controls), you'll need recursion. If they aren't nested, you just need to loop over the container's Controls collection.
Code:
' This works if children are not nested
For Each child As Control In ContainingPanelOrTableOrWhateverTheCaseMayBe.Controls
    DoWhatWeNeedToWith(child)
Next

If you do need recursion, and you aren't familiar with the topic, read a tutorial. Then apply what you learn to the situation at hand. Here is a start:
Code:
Sub DoSomethingWithAllContainedPanels(ControlCollection controls)
    For Each child As Control In controls
        If child Is Panel
            DoWhatWeNeedToWith(child)
        End If
 
        If child.Controls.Count > 0 Then
            ' Child is a container and might contain panels
            ' Todo: loop over the child's controls (RECURSION)
        End If
    Next
End Sub
 
Top Bottom