Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C、 两个按钮启动一个方法时出现问题_C#_Winforms_Button - Fatal编程技术网

C# C、 两个按钮启动一个方法时出现问题

C# C、 两个按钮启动一个方法时出现问题,c#,winforms,button,C#,Winforms,Button,如果你来自美国,并且你去过一个爆竹桶,那么你可能玩过棋盘游戏,在那里你必须跳出钉子,直到你只剩下一个为止。它类似于中国跳棋,只有一个金字塔或三角形 我有一个制作按钮并将其添加到表单中的表单,我有一个TheBoard类,该类包含跳跃如何在表单上工作的所有规则。在我的表单中,我还有一个按钮点击器方法,需要运行所有这些 我好像撞到了砖墙。我无法理解让它接受第二次点击,以便在board类中遍历整个if语句背后的逻辑。board类中move方法的参数采用int x,这是作为参数单击的按钮。我觉得我错过了下

如果你来自美国,并且你去过一个爆竹桶,那么你可能玩过棋盘游戏,在那里你必须跳出钉子,直到你只剩下一个为止。它类似于中国跳棋,只有一个金字塔或三角形

我有一个制作按钮并将其添加到表单中的表单,我有一个TheBoard类,该类包含跳跃如何在表单上工作的所有规则。在我的表单中,我还有一个按钮点击器方法,需要运行所有这些

我好像撞到了砖墙。我无法理解让它接受第二次点击,以便在board类中遍历整个if语句背后的逻辑。board类中move方法的参数采用int x,这是作为参数单击的按钮。我觉得我错过了下半个动作。我如何让我的移动方法注册两个按钮点击的开始位置的销钉和结束位置的销钉

表格编号:

public partial class Form1 : Form
{
    private Button[] btn = new Button[15];
    private TheBoard myboard = new TheBoard();

    public Form1()
    {
        InitializeComponent();

        int buttonsPerRow = 1;
        int index = 0;

        while (index < btn.Length)
        {
            int increment = this.Width / (buttonsPerRow + 1);

            for (int j = 1; j <= buttonsPerRow; j++)
            {
                btn[index] = new Button
                {
                    //other style elements of the button
                    Name = "btn" + index
                } 


                btn[index].Click += new EventHandler(this.My_Click);
                Controls.Add(btn[index]);

                index++;
            }

            buttonsPerRow++;
        }
    }

    private void My_Click(object sender, EventArgs e) {

    myboard.getValues();
    Button b = (Button)sender;
    string bName = b.Name;
    // Now pull off the btn
    string num = bName.Substring(3, bName.Length - 3);
    // Parsing the number to an int
    int x = Int32.Parse(num);
    myboard.move(x);

    int[] color = myboard.getValues();
    for (int i = 0; i < 15; i++)
    {
        color = myboard.getValues();
        if (color[i] == TheBoard.hasPeg)
        {
            btn[i].BackColor = System.Drawing.Color.Yellow;
        }
        else
            btn[i].BackColor = System.Drawing.Color.Black;
    }//for



    }
}
董事会类别代码:

class TheBoard
{
  static public int hasPeg = 100;
    static public int noPeg = 50;
    private int[] board;
    private int firstMove; //1st click

    public TheBoard()
    {
        board = new int[15];
        board[0] = noPeg;
        for(int i = 1; i < 15; i++)
        {
            board[i] = hasPeg;
        }
        firstMove = -1; //giving last move a location, starting it at the beginning
    }

    public int move(int x)
    {
        if(firstMove == -1)
        {
            firstMove = x;
            return 0;
        }
        // blank at 0


        // if you click a blank your 1st move
        if (firstMove == noPeg)
        {
            Console.WriteLine("You cant move if there isn't a peg.");
            return 666;
        }


        // first---------------------------------------middle-----------------------end
        if (firstMove == 1 && board[0] == hasPeg && board[3] == hasPeg && board[6] == noPeg)
        {
            RemovePeg(board[0], board[3], board[6]);
            return 0;
        }
        if (firstMove == 1 && board[0] == hasPeg && board[2] == hasPeg && board[4] == noPeg)
        {
            RemovePeg(board[0], board[2], board[4]);
            return 0;
        }
        //etc for remaining firstMove possibilities

        firstMove = -1;
        return 5;
    }

    private int RemovePeg(int first, int second, int goal) {
        board[goal] = hasPeg;
        board[first] = noPeg;
        board[second] = noPeg;
        return 0;
    }

    public int[] getValues()
    {
        return board;
    }
}

我看过代码,我想我理解你的问题;你可以选择起始桩,但你没有办法选择它应该去哪里。在对代码进行最小编辑的情况下,我会将第一个按钮单击存储在全局变量中,然后第二个按钮单击知道它是第二个按钮,并使用这两条信息启动电路板移动并重置全局变量。

您应该在按钮中指定onclick事件!我是新手,所以我不太清楚你的意思。。在创建每个按钮的for循环中,或者在my_Click In form1?中,我需要在我的move方法中有两个参数,或者需要在其中的唯一参数是第二次单击?我将有两个。从和到。move方法在第二次单击之前不会被调用。我可以使用一个标志来查看它是第一次单击还是第二次单击?就像count=0一样,它执行第一次单击,然后将count设置为1,并有一条语句根据两次单击的输入运行move方法?是的,听起来不错。我将存储count=0,然后在按下按钮时检查count=0{count=button的索引;}否则{movecount,new index;count=0;}