// // Constructor // public PlayerGroupBox(Player playerObject) { player = playerObject; // define groupbox properties Size = new Size(GROUPBOX_WIDTH, GROUPBOX_HEIGHT); // fixed size Font = new Font("Segoe UI", 9F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(0))); // name textbox near top playerNameTextBox = new TextBox() { Size = new Size(TEXTBOX_WIDTH, 32), Location = new Point(ClientSize.Width / 2 - TEXTBOX_WIDTH / 2, 30), // center textbox TextAlign = HorizontalAlignment.Center, Font = new Font("Segoe UI", 14.25F, FontStyle.Bold, GraphicsUnit.Point, ((byte)(0))), TabIndex = 1, Text = PlayerName, ForeColor = TileColor.Darker, }; playerNameTextBox.TextChanged += new EventHandler(PlayerNameTextBox_TextChanged); playerNameTextBox.KeyPress += new KeyPressEventHandler(PlayerNameTextBox_KeyPress); this.Controls.Add(playerNameTextBox); // color swatches from the available choices for (int i = 0; i < swatches.Length; i++) { swatches[i] = new ColorSwatchButton() { // 0th swatch's x offset is at parent width - (half the number of swatches * spacing) // add i*spacing and swatches will be horizontally centered regardless how many swatches exist Location = new Point(ClientSize.Width / 2 - (swatches.Length * SWATCH_SPACING) / 2 + (i * SWATCH_SPACING), 75), TileColor = colorChoices[i], Selected = colorChoices[i] == TileColor, // select the one chosen by the player }; swatches[i].Click += new EventHandler(ColorSwatchButton_Click); this.Controls.Add(swatches[i]); } // checkbox "Computer player" near bottom isComputerCheckBox = new CheckBox() { Font = new Font("Segoe UI", 11.25F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(0))), Location = new Point(60, 115), AutoSize = true, Size = new Size(225, 25), TabIndex = 2, Text = "Computer player", UseVisualStyleBackColor = true, Checked = IsComputer, }; isComputerCheckBox.CheckedChanged += new EventHandler(IsComputerCheckBox_CheckChanged); this.Controls.Add(isComputerCheckBox); }
// // Event Handlers // // event raised when any color swatch in the groupbox is clicked private void ColorSwatchButton_Click(object sender, EventArgs e) { ColorSwatchButton clickedSwatch = sender as ColorSwatchButton; // go through each color swatch and if sender was one clicked, // save it to SelectedColor (public property) and recolor playerName foreach (ColorSwatchButton swatch in swatches) { if (clickedSwatch == swatch) // this swatch is the one clicked { TileColor = swatch.TileColor; // this property's setter will raise a PropertiesChanged event } } soundClick.Play(); // play a "click" sound }