消息框在计时器中反复弹出
本文关键字:计时器 消息 | 更新日期: 2023-09-27 18:16:27
我正在制作著名的"井字棋"游戏
我遇到一个消息框反复出现的问题。
private void button1_Click(object sender, EventArgs e)
{
button1.Text = turn;
if (turn=="x")
{
turn = "0";
}
else if (turn=="0")
{
turn = "x";
}
button1.Enabled = false;
}
private void button2_Click(object sender, EventArgs e)
{
button2.Text = turn;
if (turn == "x")
{
turn = "0";
}
else if (turn == "0")
{
turn = "x";
}
button2.Enabled = false;
}
private void button3_Click(object sender, EventArgs e)
{
button3.Text = turn;
if (turn == "x")
{
turn = "0";
}
else if (turn == "0")
{
turn = "x";
}
button3.Enabled = false;
}
private void button4_Click(object sender, EventArgs e)
{
button4.Text = turn;
if (turn == "x")
{
turn = "0";
}
else if (turn == "0")
{
turn = "x";
}
button4.Enabled = false;
}
private void button5_Click(object sender, EventArgs e)
{
button5.Text = turn;
if (turn == "x")
{
turn = "0";
}
else if (turn == "0")
{
turn = "x";
}
button5.Enabled = false;
}
private void button6_Click(object sender, EventArgs e)
{
button6.Text = turn;
if (turn == "x")
{
turn = "0";
}
else if (turn == "0")
{
turn = "x";
}
button6.Enabled = false;
}
private void button7_Click(object sender, EventArgs e)
{
button7.Text = turn;
if (turn == "x")
{
turn = "0";
}
else if (turn == "0")
{
turn = "x";
}
button7.Enabled = false;
}
private void button8_Click(object sender, EventArgs e)
{
button8.Text = turn;
if (turn == "x")
{
turn = "0";
}
else if (turn == "0")
{
turn = "x";
}
button8.Enabled = false;
}
private void button9_Click(object sender, EventArgs e)
{
button9.Text = turn;
if (turn == "x")
{
turn = "0";
}
else if (turn == "0")
{
turn = "x";
}
button9.Enabled = false;
}
private void timer1_Tick(object sender, EventArgs e)
{
if (button1.Text == "x" && button2.Text == "x" && button3.Text == "x")
{
MessageBox.Show("User 1 Won");
}
else if (button4.Text == "x" && button5.Text == "x" && button6.Text == "x")
{
MessageBox.Show("User 1 Won");
}
else if (button7.Text == "x" && button8.Text == "x" && button9.Text == "x")
{
MessageBox.Show("User 1 Won");
}
else if (button1.Text == "x" && button5.Text == "x" && button9.Text == "x")
{
MessageBox.Show("User 1 Won");
}
else if (button3.Text == "x" && button5.Text == "x" && button7.Text == "x")
{
MessageBox.Show("User 1 Won");
}
else if (button1.Text == "x" && button4.Text == "x" && button7.Text == "x")
{
MessageBox.Show("User 1 Won");
}
else if (button2.Text == "x" && button5.Text == "x" && button8.Text == "x")
{
MessageBox.Show("User 1 Won");
}
else if (button3.Text == "x" && button6.Text == "x" && button9.Text == "x")
{
MessageBox.Show("User 1 Won");
}
else if (button1.Text == "0" && button2.Text == "0" && button3.Text == "0")
{
MessageBox.Show("User 2 Won");
}
else if (button4.Text == "0" && button5.Text == "0" && button6.Text == "0")
{
MessageBox.Show("User 2 Won");
}
else if (button7.Text == "0" && button8.Text == "0" && button9.Text == "0")
{
MessageBox.Show("User 1 Won");
}
else if (button1.Text == "0" && button5.Text == "0" && button9.Text == "0")
{
MessageBox.Show("User 2 Won");
}
else if (button3.Text == "0" && button5.Text == "0" && button7.Text == "0")
{
MessageBox.Show("User 2 Won");
}
else if (button3.Text == "0" && button6.Text == "0" && button9.Text == "0")
{
MessageBox.Show("User 2 Won");
}
else if (button2.Text == "0" && button5.Text == "0" && button8.Text == "0")
{
MessageBox.Show("User 2 Won");
}
else if (button1.Text == "0" && button4.Text == "0" && button7.Text == "0")
{
MessageBox.Show("User 2 Won");
}
return;
}
如果你不禁用计时器,它将继续滴答,如果碰巧在显示另一个消息框时,它将显示另一个
您可以在tick方法中禁用计时器,并等待响应再次启用它,如下所示
timer1.Enabled = false;
MessageBox.Show();
timer1.Enabled = true;