public Form1() { InitializeComponent(); TicTacToeButton t_button = new TicTacToeButton(); // this.Controls.Add(t_button); //t_button.Location = new Point(25, 25); //t_button.Location = new Point(25, 25); //intialize grid with buttons int x = 0, y = 0; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { grid[i, j] = new TicTacToeButton(); this.Controls.Add(grid[i, j]); grid[i, j].Location = new Point(x, y); //y should change here y += 100; grid[i, j].Click += new EventHandler(button_Click); } //x should change here x += 100; y = 0; } }
private void button_Click(object sender, EventArgs e) { //capture the sender TicTacToeButton t = (TicTacToeButton)sender; if (xTurn) { t.BackColor = Color.LightGoldenrodYellow; t.Text = "X"; } else { t.BackColor = Color.Goldenrod; t.Text = "O"; } xTurn = !xTurn; if (CheckForWin()) { MessageBox.Show("Winner!"); } }