LamKat Posted December 4, 2011 Posted December 4, 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 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 Quote
Administrators PlausiblyDamp Posted December 5, 2011 Administrators Posted December 5, 2011 Could you elaborate on what the problem is? Is it throwing an error or simply failing to change the colour correct? Is something else happening? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
LamKat Posted December 6, 2011 Author Posted December 6, 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 Quote
Administrators PlausiblyDamp Posted December 7, 2011 Administrators Posted December 7, 2011 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. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
LamKat Posted December 7, 2011 Author Posted December 7, 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 Quote
Leaders snarfblam Posted December 7, 2011 Leaders Posted December 7, 2011 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 Quote [sIGPIC]e[/sIGPIC]
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.