How to know if all Abels with numbers are filled

Asked

Viewed 67 times

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;

Os cartões ão estes.

  • 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.

  • 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

  • 1

    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.

  • I’ve never messed with flags, I don’t know how to do that, but I can try!

  • 1

    Flag is just a "cute" name, flag is just a variable

  • 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!");

  • Yeah, it worked?

  • No! The Abels turned orange as they were supposed to, but when all the Abels with numbers turned orange, nothing else happened!

  • You can’t debug to see the behavior of your code?

  • I’m gonna try it on!

  • After all the Abels turn orange, the program continues to loop and never leaves the foreach

  • 1

    I think Vik’s response helped!

Show 7 more comments

2 answers

3


If your dashboard contains only Labels, can do the following to count how many Abels have the BackColor in orange:

int totalDeLaranjas = panel1.Controls.Cast<Label>().Count(lbl => lbl.BackColor == Color.Orange);
  • I think with this I’ll get what I want, thank you!

  • Vik, I just have a problem, I did an if(totalDeLaranjas == 15) and it works. Only when I use a Messagebox inside, the message doesn’t go away. That is, even after clicking "Ok" on the message, or trying to close it, it reappears. I understand what the problem is, but I can’t fix it

  • Without seeing the code it is difficult to know what the problem is, you can post the code of the "new number" button, or edit the post..

  • Another thing you could improve on was the creation of the cards, for example, each column having only numbers of that ten, and each column having at least one number.

  • I have this: if (totalDeLaranjas == 15) { Dialogresult dialogResult = Messagebox.Show("Player 1 won! Do you want to play again?", "Confirmation", Messageboxbuttons.Yesno); if (dialogResult == Dialogresult.Yes) { this. Close(); Form1 start = new Form1(); start.Showdialog(); } Else if (dialogResult == Dialogresult.No) { this

  • Yes, I have to do that part yet :D

  • 1

    Not knowing what is before and where it is called this code snippet is difficult... what you can think about doing is creating a method with the initial values "Setdefaultvalues()", there initializes / reset all variavies and controls and when you receive "Yes" in the message flames that method, it is not necessary to close and open the form again...

  • Um... I’ll try that, I’m sorry I’m not so clear

Show 3 more comments

2

Based on @Vik’s reply, another possible LINQ to use:

continuaWhile = 
    !panel1.Controls
        .OfType<Label>()
        .All(label => label.BackColor == Color.Orange);

and hence, in the while, will the condition:

bool continuaWhile = true;

while (continuaWhile)
{
    // Faz o que tem que fazer, aqui.

    // Por último, verifica se continua iterando o laço.
    continuaWhile =
        !panel1.Controls
            .OfType<Label>()
            .All(label => label.BackColor == Color.Orange);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.