Jozef Stone
Jozef Stone

Reputation: 23

C# foreach if loop panel color change

Im trying to change colors of panels via foreach and if loop, but after the first if loop, all colors are the same and the code is not working.

IEnumerable<Panel> pnls = panel1.Controls.OfType<Panel>();
                    foreach (Panel pan in pnls)
                    {
                        foreach (var lbls in pnls)
                        {                                                   
                            if (lbls.BackColor == Color.FromArgb(224, 224, 224))
                            {
                                lbls.BackColor = Color.FromArgb(0, 64, 0);                                                           
                            }
                           
                            if (lbls.BackColor == Color.FromArgb(0, 64, 0))
                            {
                                lbls.BackColor = Color.FromArgb(224, 224, 224);
                               
                            }                                     
                        }                           
                    }

Actually, what I want to do is this: when the button is pressed, if the panel is green, it will be red, if it is red, it will be green. I seem to understand that it is impossible to do this with this loop, how should I proceed? There is two foreach for, child panels.

Upvotes: 0

Views: 335

Answers (2)

iSR5
iSR5

Reputation: 3498

if (lbls.BackColor == Color.FromArgb(224, 224, 224))
{
    lbls.BackColor = Color.FromArgb(0, 64, 0);                                                           
}

if (lbls.BackColor == Color.FromArgb(0, 64, 0))
{
    lbls.BackColor = Color.FromArgb(224, 224, 224);
}                                     

if the first condition is true, then the second one will be also true. To separate them, you need to use else if like this :

if (lbls.BackColor == Color.FromArgb(224, 224, 224))
{
    lbls.BackColor = Color.FromArgb(0, 64, 0);                                                           
}
else if (lbls.BackColor == Color.FromArgb(0, 64, 0))
{
    lbls.BackColor = Color.FromArgb(224, 224, 224);
}                                     

this will ensure that BackColor will be set once.

The other thing that you have is the looping. Both are looping over panels, so if you have for instance 5 panels, then your looping would iterates about 25 times. What I think you're looking for is that you need to loop over all panels, and then on each panel loop over all Labels. which makes sense.

here is a revised version :

foreach (Panel panel in panel1.Controls.OfType<Panel>())
{
        if (panel.BackColor == Color.FromArgb(224, 224, 224))
        {
            panel.BackColor = Color.FromArgb(0, 64, 0);                                                           
        } 
        else  if (panel.BackColor == Color.FromArgb(0, 64, 0))
        {
            panel.BackColor = Color.FromArgb(224, 224, 224);        
        }
}

Upvotes: 1

KiynL
KiynL

Reputation: 4266

mybe else solve the problem:

    if (lbls.BackColor == Color.FromArgb(224, 224, 224))
    {
        lbls.BackColor = Color.FromArgb(0, 64, 0);                                                           
    }
    else if (lbls.BackColor == Color.FromArgb(0, 64, 0))
    {
        lbls.BackColor = Color.FromArgb(224, 224, 224);
                           
    }  

Upvotes: 1

Related Questions