C# 引用类函数中的形式

C# 引用类函数中的形式,c#,C#,这是我的CheckXWinner函数代码,在这里我需要引用我的表单以绘制赢线: public void CheckXWinner(Button[] buttonArray, Form1 frm) { int arrLength = buttonArray.Length; int root = (int)Math.Sqrt(Convert.ToDouble(arrLength)); bool winner = false;//var

这是我的
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

该调用的Form1部分有错误,如何修复此问题?

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

CheckXWinner(buttonArray, a_ref_to_form);
但是,看看您的
CheckXWinner
实现,您似乎并没有在任何地方引用
frm

将CheckXWinner的声明重写为

public void CheckXWinner(Button[] buttonArray)
这样称呼它:

CheckXWinner(buttonArray);

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


通过“this”而不是Form1 frm。

有什么错误?!?你是以哪种形式处理按钮点击的?我有一个Gameboard类,所有这些都在其中,与表单分开。按钮点击在Gameboard类的构造函数之外,所以是CheckXWinner,所以onyeah在那里,所以我可以在绘制winline时引用它(尚未编写该部分)。请参阅我下面的评论,我尝试输入“this”,但我得到了一个错误。我在发布之前就尝试过这个,这就是为什么我对我在这里做错了什么感到困惑的原因我正在传递表单引用,因为我需要在绘制动态winlines的函数中引用它。对于代码,我需要将表单引用为画布等。我还没有编写该代码,我只是注意到我没有得到一个干净的构建,并且无法找出原因。与CheckXWinner(System.Windows.Forms.Button[],MyProgram.Form1)匹配的最佳重载方法有一些无效参数,而不是ButtonRay尝试传递“b”。当我传入的只是buttonarray,并且函数定义中没有涉及表单引用时,函数运行良好。在我将表单引用添加到定义之后,当我尝试调用它时,函数会给我错误
CheckXWinner(buttonArray);