2
I have a card with randomly generated numbers that when they are equal to the number generated in the middle, make the orange Labels.
However, I wanted to do something for when all the Abels with numbers turned orange, a MessageBox
saying something. But I don’t know if these are all orange. How do I do that?
I only have this for when the text of the Labels is equal to the middle number.
foreach (Label lblCor in panel1.Controls)
if (lblNum.Text == lblCor.Text)
lblCor.BackColor = Color.Orange;
It would not be because you are trying to put your logic inside if (lblNum.Text == lblCor.Text), that is, it will only check when the middle number is equal.
– Wictor Chaves
Maybe that’s it... I tried to do it another way, but I couldn’t. I tried to create an int with a size of 15 and whenever a label turned orange took 1 in the int. So when I was 0, a Messagebox appeared, but it didn’t work
– D. Rodrigues
You can create a flag outside the for with the name shows Match = true and inside the for you put the following if (lblCor.Backcolor != Color.Orange), if it enters this if you arrow the flag to false, and below the for you put another if checking the flag, ie, if it enters the if inside the for is it is because have not been filled all the Labels with orange.
– Wictor Chaves
I’ve never messed with flags, I don’t know how to do that, but I can try!
– D. Rodrigues
Flag is just a "cute" name, flag is just a variable
– Wictor Chaves
This is what I understood: var showMensangem = true; foreach (Label lblCor in Panel1.Controls) { if (lblNum.Text == lblCor.Text) lblCor.Backcolor = Color.Orange; if (lblCor.Backcolor != Color.Orange) mostraMensangem = false; } if (mostraMensangem == true) Messagebox.Show("Won!");
– D. Rodrigues
Yeah, it worked?
– Wictor Chaves
No! The Abels turned orange as they were supposed to, but when all the Abels with numbers turned orange, nothing else happened!
– D. Rodrigues
You can’t debug to see the behavior of your code?
– Wictor Chaves
I’m gonna try it on!
– D. Rodrigues
After all the Abels turn orange, the program continues to loop and never leaves the foreach
– D. Rodrigues
I think Vik’s response helped!
– D. Rodrigues