只显示一个消息框

时间:2016-03-20 20:52:18

标签: c# boolean messagebox single-instance

我试图只使用gameover bool显示一个消息框来控制情况,但它不起作用,它显示无限的消息框。

bool player2turn = false;
bool gameover = false;


private void timer1_Tick(object sender, EventArgs e)
 {

  if ((pb00.BackColor == pb10.BackColor) && (gameover == false))
     {
       winner();               
     }


 }

private void winner()
 {

  if (player2turn == false)
     {
       MessageBox.Show("Player 2 wins!", "Retry?", MessageBoxButtons.YesNo);
       gameover = true;
      }
  else
      {
       MessageBox.Show("Player 1 wins!", "Retry?", MessageBoxButtons.YesNo);
       gameover = true;

      }

  }

2 个答案:

答案 0 :(得分:0)

MessageBox.Show阻止执行代码,直到您关闭消息框,因此您需要在调用MessageBox.Show之前移动gameover分配。

private void winner()
 {

  if (player2turn == false)
     {
       gameover = true;
       MessageBox.Show("Player 2 wins!", "Retry?", MessageBoxButtons.YesNo);       
      }
  else
      {
       gameover = true;
       MessageBox.Show("Player 1 wins!", "Retry?", MessageBoxButtons.YesNo);       
      }
  }

答案 1 :(得分:0)

@HansPassant谢谢

private void winner()
{

if (player2turn == false)
  {
   timer1.Stop();
   MessageBox.Show("Player 2 wins!", "Retry?", MessageBoxButtons.YesNo);
   gameover = true;
  }
else
  {
   timer1.Stop();
   MessageBox.Show("Player 1 wins!", "Retry?", MessageBoxButtons.YesNo);
   gameover = true;

  }

}