消息框在计时器中重复弹出

时间:2016-11-04 19:16:51

标签: c# messagebox

我正在创造着名的游戏Tic Tac Toe。

我遇到问题,反复出现消息框。

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;
    }

1 个答案:

答案 0 :(得分:1)

如果您没有禁用计时器,它将继续打勾,如果恰好是在显示另一个消息框时,它将显示另一个

您可以在tick方法中禁用计时器,并等待响应再次启用它,如此

    <script>
//That is function which is called through javascript Onclick Event
            function myMapStyle(style,id){

            L.mapbox.accessToken ='pk.eyJ1Ijoic2FkZGFtMjIxIiwiYSI6ImNpdjB0cnF6NTAwMzAyem1rMmhhOHdib2YifQ.Q7Z7bH77Mdf2W8k78O7ilQ';
                        var map = L.mapbox.map('map',id,{
                            center : [31.450112, 74.308888],
                            zoom: 13,
                        });


                        L.mapbox.tileLayer('mapbox.'+style,{
                            attribution: 'Map Data',
                            maxZoom:18,
                        }).addTo(map);
                    };
     </script>