// raised when Enter key is pressed while inside a PlayerGroupBox playerName text box. // from player 1 it moves to player 2; from player 2 it moves to the start button if it's enabled // or back to player 1 if the start button is not enabled. private void PlayerGroupBox_EnterKeyPressed(object sender, EventArgs e) { PlayerGroupBox groupBox = sender as PlayerGroupBox; // go through all but the last groupbox, and if that was the one that raised the event, tab to next groupbox for (int i = 1; i < playerGroupBoxes.Length - 1; i++) { if (groupBox == playerGroupBoxes[i]) { SelectNextControl(playerGroupBoxes[i + 1], true, true, true, true); } } // if the last groupbox raised the event, tab to start button if enabled, or the first groupbox otherwise if (groupBox == playerGroupBoxes[playerGroupBoxes.Length - 1]) { if (startButton.Enabled) { startButton.Select(); } else { SelectNextControl(playerGroupBoxes[1], true, true, true, true); } } }
// create controls and add them to this panel private void AddControls() { // "Welcome to Tile Tac Toe" header at the top of the panel titleLabel = new Label() { Font = new Font("Segoe UI", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))), Size = new Size(280, 30), Text = "Welcome to Tile Tac Toe", TextAlign = ContentAlignment.MiddleCenter, // text centered within control Anchor = AnchorStyles.Top, // when parent dock fills and changes size, this will keep it centered to top }; this.Controls.Add(titleLabel); // add a groupbox for each player except 0th player for (int i = 1; i < playerGroupBoxes.Length; i++) { playerGroupBoxes[i] = new PlayerGroupBox(TileTacToe.Players[i]) { Anchor = AnchorStyles.Top, Text = "Player " + i + " choose a name and color", TabIndex = i + 1, }; this.Controls.Add(playerGroupBoxes[i]); } // combo box to decide who goes first whoFirstComboBox = new ComboBox() { DropDownStyle = ComboBoxStyle.DropDownList, Font = new Font("Segoe UI", 11.25F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(0))), FormattingEnabled = true, Size = new Size(100, 30), Anchor = AnchorStyles.Top, TabIndex = playerGroupBoxes.Length + 2, }; whoFirstComboBox.Items.AddRange(new object[] { "Coin Toss", "Player 1", "Player 2" }); whoFirstComboBox.SelectedIndex = 0; // make first option default ("Coin Toss") whoFirstComboBox.SelectedIndexChanged += new EventHandler(ComboBox_SelectedIndexChanged); this.Controls.Add(whoFirstComboBox); // label that reads "Who goes first?" to the left of the combo box whoFirstLabel = new Label() { Font = new Font("Segoe UI", 11.25F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(0))), Size = new Size(200, 28), Text = "Who goes first?", TextAlign = ContentAlignment.MiddleRight, Anchor = AnchorStyles.Top, }; this.Controls.Add(whoFirstLabel); // combo box for difficulty difficultyComboBox = new ComboBox() { DropDownStyle = ComboBoxStyle.DropDownList, Font = new Font("Segoe UI", 11.25F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(0))), FormattingEnabled = true, Size = new Size(100, 30), Anchor = AnchorStyles.Top, TabIndex = playerGroupBoxes.Length + 3, }; // with only two options, a checkbox may be better; but may add more difficulties later difficultyComboBox.Items.AddRange(new object[] { "Easy", "Hard" }); difficultyComboBox.SelectedIndex = TileTacToe.Difficulty; // set default to the one defined in TileTacToe.cs difficultyComboBox.SelectedIndexChanged += new EventHandler(ComboBox_SelectedIndexChanged); this.Controls.Add(difficultyComboBox); // label that reads "Difficulty" to the left of the combo box difficultyLabel = new Label() { Font = new Font("Segoe UI", 11.25F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(0))), Size = new Size(200, 28), Text = "Computer difficulty", TextAlign = ContentAlignment.MiddleRight, Anchor = AnchorStyles.Top, }; this.Controls.Add(difficultyLabel); // label in red that informs user why the start button is disabled ("Both players need unique colors" or "Both players need a name") warningLabel = new Label() { Font = new Font("Segoe UI", 11.25F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(0))), Size = new Size(280, 28), ForeColor = Color.Red, TextAlign = ContentAlignment.MiddleCenter, Anchor = AnchorStyles.Top, }; this.Controls.Add(warningLabel); // Start button to begin a game startButton = new Button() { Font = new Font("Segoe UI Semibold", 12F, FontStyle.Bold, GraphicsUnit.Point, ((byte)(0))), Size = new Size(125, 40), Text = "Start", UseVisualStyleBackColor = true, Anchor = AnchorStyles.Top, TabIndex = 1, }; this.Controls.Add(startButton); }