Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

       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

Posted

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

  • Administrators
Posted

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.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

  • Leaders
Posted

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.

' 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:

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

[sIGPIC]e[/sIGPIC]

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...