Make a loop for 2 cells by clicking

Asked

Viewed 46 times

1

I need a loop for 2 Labels where each one appears a text already specified in the code. Just out of curiosity. I wanted a loop with the same effect as the code below:

private void button1_Click(object sender, EventArgs e)
{
     lbl_1.Text = "Ziummmmmm";

     ++NumberOfClick;
     switch(NumberOfClick)
     {
          case 1:
              lbl_1.Text = "Ziummmmmm";
              break;
          case 2:
              lbl_2.Text = "Ploft";
              break;
          case 3:
              lbl_1.Text = "";
              lbl_2.Text = "";
              break;
     } 
}

I’m trying to make a loop, but it’s incomplete and I don’t know how to continue, any help I accept.

while (lbl_1.Text == "Ziummmmmm")
{
      lbl_2.Text = "Ploft";
      while (lbl_2.Text == "Ploft")
      {
           lbl_1.Text = "";
           lbl_2.Text = ""; 
      }              
}

1 answer

0

The only reason why you see the change from one text to the other, is the fact that it happens under the condition of occurrence of an event (which in the case of you click the mouse button). If you change the texts in a loop, all you will see will be the last text changed/updated (As there was no condition to "hold" the toggle, no event, it simply updated to the end). So your reasoning makes no sense.

However, you could do that:

int i = 0;
while (i < 3)
{
    if (i == 1)
    {
        lbl_1.Text = "Ziummmmmm";  
    } 
    else if (i == 2)
    {
        lbl_2.Text = "Ploft";  
    } 
    else if (i == 3)
    {
        lbl_1.Text = "";
        lbl_2.Text = "";
    }
    i++;
}

In your first example, you update the variable i (which in this case is Numberofclick) at each mouse click. The way you want it, the variable is updated with each iteration / loop. That is, all you should see on the Label, finally, are just the final results, which in this case are empty Strings ("", "").

  • 1

    Now I get it, thank you very much man, it worked out :D!!

  • Don’t forget to accept my answer as correct! Wow.

Browser other questions tagged

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