Home > Blockchain >  C# foreach if loop panel color change
C# foreach if loop panel color change

Time:05-08

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.

CodePudding user response:

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);
                           
    }  

CodePudding user response:

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>())
{
    foreach (var label in panel.Controls.OfType<Label>())
    {             
        if (label.BackColor == Color.FromArgb(224, 224, 224))
        {
            label.BackColor = Color.FromArgb(0, 64, 0);                                                           
        } 
        else  if (label.BackColor == Color.FromArgb(0, 64, 0))
        {
            label.BackColor = Color.FromArgb(224, 224, 224);        
        }
    }                           
}
  • Related