-2

这是我的CheckXWinner函数代码,我需要在其中引用我的表单才能绘制胜利线:

    public void CheckXWinner(Button[] buttonArray, Form1 frm)
    {
        int arrLength = buttonArray.Length; 
        int root = (int)Math.Sqrt(Convert.ToDouble(arrLength));
        bool winner = false;//variable to keep the computer from going when Xwins
        for (int i = 0;  i < root;  i++)
        {
            //Sets the counter for the winners back to zero
            int d2Count = 0;
            int d1Count = 0;
            int hCount = 0;
            int vCount = 0;

                for(int j = 0;  j < root; j++)
                {
                    //increments the appropriate counter if the button contains an X
                    //Horizonal win
                    if (buttonArray[(i*root) + j].Text == "X")
                    {
                        hCount++;
                        if (hCount == root)
                        {
                            for (int z = (root - 1); z >= 0; z--)
                            {
                                buttonArray[(i*root) + z].BackColor = Color.IndianRed;
                            }
                            Xwins();
                            winner = true; //sets winner to true so computer does not take turn 
                        }
                    }//end of Horizonal win

                    //Left to right diagonal
                    if (buttonArray[j + (j*root)].Text == "X")
                    {
                        d1Count++;
                        if (d1Count == root)
                        {
                            for (int z = (root - 1); z >= 0; z--)
                            {
                                buttonArray[z + (z * root)].BackColor = Color.IndianRed;
                            }
                            Xwins();
                            winner = true; 
                        }
                    }//end of LTR win

                    //Right to left diagonal
                    if (buttonArray[(j*(root - 1)) + (root - 1)].Text == "X")
                    {
                        d2Count++;
                        if (d2Count == root)
                        {
                            for (int z = (root - 1); z >= 0; z--)
                            {
                                buttonArray[(z*(root - 1)) + (root - 1)].BackColor = Color.IndianRed;
                            }
                            Xwins();
                            winner = true; 
                        }
                    }//end of RTL win

                    //Vertical win
                    if (buttonArray[i + (root*j)].Text == "X")
                    {
                        vCount++;
                        if (vCount == root)
                        {
                            for (int z = (root - 1); z >= 0; z--)
                            {
                                buttonArray[i + (root*z)].BackColor = Color.IndianRed;
                            }
                            Xwins();
                            winner = true; 
                        }
                    }//end of vert win                        
                }//end of for j loop
        }//end of for loop
        CheckDraw();
        if (winner == false)
        {
            ComputerGoes(buttonArray);
        };

    }//end of CheckXWinner

在这个类的另一部分,我有与表单关联的所有按钮点击的处理程序:

    //Handle any button clicks
    private void button_click(object sender, EventArgs e)
    {
        Button b = (Button)sender;
        b.Text = "X";
        b.Enabled = false;
        CheckXWinner(buttonArray, Form1 frm);
    }

我对该调用的 Form1 部分有错误,我该如何解决?

4

2 回答 2

1

在您传递的地方,Form1 frm您实际上应该传递对 Form1 上的实例的引用。根据您对传递建议的评论,this按钮处理程序似乎是在另一个表单中声明的​​(而不是在 Form1 中)。如果是这种情况,您应该获取/保留对 Form1 实例的引用并将其传入:

CheckXWinner(buttonArray, a_ref_to_form);

但是,查看您的实现CheckXWinner看起来并不像您在frm任何地方引用!

将 CheckXWinner 的声明重写为

public void CheckXWinner(Button[] buttonArray)

并这样称呼它:

CheckXWinner(buttonArray);
于 2013-03-10T17:50:11.030 回答
0

当您将 frm 作为参数传递时,它不会在任何地方声明。

传递“this”而不是 Form1 frm。

于 2013-03-10T17:54:42.093 回答