Hi, Mr.Coder! Welcome here.
I'm looping through my Labels on line 11 until line 18 of my code.
Your loop may be a little different, depending on your specific situation.
I notice now I forgot the braces in the foreach loop.
Because I only have one if statement in it this is allowable, but IMHO the compiler should give a warning.
ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
Uh, hi. I tried this code to change the background images of buttons, but it never seems to get past the 'if (Cntrl is Panel)' part
foreach (Control Cntrl in this.pnlDeadPcs.Controls)
{
if (Cntrl is Panel)
{
foreach (Control C in Cntrl.Controls)
{
if (C is Button)
{
MessageBox.Show("In if C Button");
if (iPlus < 10)
{
pieceName = "App_Pics/piece_" + "0" + iPlus.ToString() + "_b.png";
C.BackgroundImage = Image.FromFile(pieceName);
}
else
{
pieceName = "App_Pics/piece_" + iPlus.ToString() + "_b.png";
C.BackgroundImage = Image.FromFile(pieceName);
}
iPlus++;
}
}
}
}
iPlus is simply my way of telling how many times the foreach loop ran. It is also concatenated to the image name that is to be used for the background image of the button (button 1 uses image 1, and button 2 uses image 2).
I'm not sure if that's entirely possible, though, as I have little experience with foreach loops. Perhaps I could make / research a for loop version.
But my problem right now is that the code never gets past the first if. The difference I see there between your code and the one I used is that I used an actual Panel (pnlDeadPcs), instead of a Tab page. Is there something I did wrong?
zack_falcon
Junior Poster in Training
52 posts since Jul 2010
Reputation Points: 10
Solved Threads: 1
With nested controls you must be aware which Controls collection you're dealing with.
Also, Panel control doesn't support foreach syntax so you have to use for-loop
Control c;
for (int i=0; i<pnlDeadPcs.Controls.Count; i++)
{
c = pnlDeadPcs.Controls[i];
if (c is Button)
{
// Use Button c
}
}
HTH
Teme64
Veteran Poster
1,031 posts since Aug 2008
Reputation Points: 218
Solved Threads: 203