为什么我的disableButtons方法不起作用?

时间:2015-06-15 09:01:25

标签: c#

我目前正在为一个C#的学校项目工作,我正在制作Tic Tac Toe。 我有一点问题..

我的代码过去工作得非常好,直到我在记分板中构建。

我认为记分牌正在推翻disableButtons ..在我添加记分牌之前,它的工作非常好......

有人可以查看我的代码并告诉我错误吗?

它应该像这样工作:

播放,如果有胜利者,请禁用按钮。请帮帮我!

编辑:我不知道为什么我的命名空间在此文本字段中..

namespace Tic_Tac_Toe
{
    public partial class Form1 : Form
    {
        bool turn = true; //true = x en false = o
        int turn_count = 0;
        static String player1, player2;

        public Form1()
        {
            InitializeComponent();
        }

        public static void setPlayerName(String n1, String n2)
        {
            player1 = n1;
            player2 = n2;
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Gemaakt door Luca Fraser", "About");
        }

        private void button_click(object sender, EventArgs e)
        {
            Button b = (Button)sender;
            if (turn)
                b.Text = "X";
            else
                b.Text = "O";

            turn = !turn;
            b.Enabled = false;
            turn_count++;
            checkForWinner();

            if (turn_count % 2 == 0)
            {
                lblTurn.Text = player1 + " is aan de beurt";
            }
            else
            {
                lblTurn.Text = player2 + " is aan de beurt";
            }
        }

        private void checkForWinner()
        {
            bool thereIsWinner = false;

            //Horizontaal checken
            if((btnA1.Text == btnA2.Text) && (btnA2.Text == btnA3.Text) && (!btnA1.Enabled))
                thereIsWinner = true;
            else  if((btnB1.Text == btnB2.Text) && (btnB2.Text == btnB3.Text) && (!btnB1.Enabled))
                thereIsWinner = true;
            else if((btnC1.Text == btnC2.Text) && (btnC2.Text == btnC3.Text) && (!btnC1.Enabled))
                thereIsWinner = true;

            //Verticaal checken
            if ((btnA1.Text == btnB1.Text) && (btnB1.Text == btnC1.Text) && (!btnA1.Enabled))
                thereIsWinner = true;
            else if ((btnA2.Text == btnB2.Text) && (btnB2.Text == btnC2.Text) && (!btnA2.Enabled))
                thereIsWinner = true;
            else if ((btnA3.Text == btnB3.Text) && (btnB3.Text == btnC3.Text) && (!btnA3.Enabled))
                thereIsWinner = true;

            //Diagonaal checken
            if ((btnA1.Text == btnB2.Text) && (btnB2.Text == btnC3.Text) && (!btnA1.Enabled))
                thereIsWinner = true;
            else if ((btnA3.Text == btnB2.Text) && (btnB2.Text == btnC1.Text) && (!btnC1.Enabled))
                thereIsWinner = true;


            if (thereIsWinner)
            {
                disableButtons();

                String winner = "";
                if (turn)
                {
                    winner = player2;
                    lblO.Text = (Int32.Parse(lblO.Text) + 1).ToString();
                }
                else
                {
                    winner = player1;
                    lblX.Text = (Int32.Parse(lblX.Text) + 1).ToString();
                }
                MessageBox.Show(winner + " wins!");
            }
            else
            {
                if (turn_count == 9)
                {
                    lblDraw.Text = (Int32.Parse(lblDraw.Text) + 1).ToString();
                    MessageBox.Show("It's a draw!");
                }
            }

        }

        private void disableButtons()
        {
            try
            {
                foreach (Control c in Controls)
                {
                    Button b = (Button)c;
                    b.Enabled = false;
                }
            }
            catch { }
        }

        private void newGameToolStripMenuItem_Click(object sender, EventArgs e)
        {
            turn = true;
            turn_count = 0;

                foreach (Control c in Controls)
                {
                    try
            {
                        Button b = (Button)c;
                        b.Enabled = true;
                        b.Text = "";
                }
                    catch { }
            }

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Form2 formtwo = new Form2();
            formtwo.ShowDialog();
            lblXcount.Text = player1;
            lblOcount.Text = player2;
            lblTurn.Text = player1 + " is aan de beurt";
        }

        private void mouse_enter(object sender, EventArgs e)
        {
            Button b = (Button)sender;
            if (b.Enabled)
            {
                if (turn)
                {
                    b.Text = "X";
                }
                else
                    b.Text = "O";
            }
        }

        private void mouse_leave(object sender, EventArgs e)  
        {
             Button b = (Button)sender;
             if (b.Enabled)
            {
                b.Text = "";
            }

        }

        private void resetWinCountToolStripMenuItem_Click(object sender, EventArgs e)
        {
            lblO.Text = "0";
            lblX.Text = "0";
            lblDraw.Text = "0";
        }

        private void lblOcount_Click(object sender, EventArgs e)
        {

        }

        private void lblOcount_TextChanged(object sender, EventArgs e)
        {

        }
        }
    }

3 个答案:

答案 0 :(得分:4)

下面:

private void disableButtons()
{
    try
    {

        foreach (Control c in Controls)
        {
            Button b = (Button)c;
            b.Enabled = false;
        }
    }
    catch { }
}

当你遍历一个不是Button的控件时,你会遇到异常并且循环结束。

你可以试试这个:

    foreach (Control c in Controls)
    {
        Button b = c as Button;
        if(b != null)
            b.Enabled = false;
    }

答案 1 :(得分:2)

您的现有代码尝试将每个控件解析为Button,如果它失败并抛出异常。你抓住它并没有任何反应。

尝试使用此代码并仅获取Button s来禁用:

foreach (Button btn in this.Controls.OfType<Button>())
{
    btn.Enabled = false;
}

建议:在您开发过程中评论catch块代码,因为它很容易在异常发生的时间和地点进行调试。

答案 2 :(得分:0)

String postData = "session=" + session;
    webView.postUrl(urlPassedFromArguments,
            EncodingUtils.getBytes(postData, "base64"));

    webView.setWebViewClient(new WebViewClient() {

        @Override
        public void onReceivedError(WebView view, int errorCode,
                String description, String failingUrl) {

            if (isAdded() && getActivity() != null)
                Toast.makeText(getActivity(), description,
                        Toast.LENGTH_SHORT).show();

        }

        @Override
        public void onPageFinished(WebView view, String url) {
            try {
                dialog.dismiss();
            } catch (Exception e) {
            }
            super.onPageFinished(view, url);
            Logger.e("onPageFinished url", url);

            if (!resolvedUrlLoaded) {
                String postData = "token=" + token;
                webView.postUrl(url,
                        EncodingUtils.getBytes(postData, "base64"));
                resolvedUrlLoaded = true;
            }
        }

    });

您需要检查控件是否为按钮,否则将导致抛出异常。

这是一个很好的例子,说明为什么它是 BAD 想要吞下异常。如果您报告或记录了异常,问题就会明显。