public void SetCell(int col, int row, int value) { // Find the Label control var subgrid = SudokuPuzzle.GetRegion(col, row); var labelName = $"Label{col}{row}{subgrid}"; var control = tableLayoutPanel1.Controls.Find(labelName, false).FirstOrDefault(); var label = (SudokuLabel)control; if (label == null) { return; } if (value > 0) { label.Value = value; label.Font = new Font("Consolas", 14, FontStyle.Bold); SudokuPuzzle.SetAlternateRegionColors(ref label); label.Text = $@"{value}"; } else if (value == 0) { // set the appearance for the Label control label.Font = new Font("Consolas", 8); SudokuPuzzle.SetAlternateRegionColors(ref label); label.MouseDown += SudokuLabel_MouseDown; } }
public void DrawBoard() { //if (tableLayoutPanel1.Controls.Count >= 81) throw new Exception("The board has already been drawn."); // used to store the location of the cell var location = new Point(); // draws the cells for (var row = 1; row <= 9; row++) { for (var col = 1; col <= 9; col++) { location.X = col * (CellWidth + 1) + XOffset - 8; location.Y = row * (CellHeight + 1) + YOffset - 28; var subgrid = SudokuPuzzle.GetRegion(col, row); var sudokuLabel = new SudokuLabel { Name = $"Label{col}{row}{subgrid}", Tag = $"Label{col}{row}{subgrid}", Font = new Font("Consolas", 8, FontStyle.Bold), BorderStyle = BorderStyle.Fixed3D, Location = location, Width = CellWidth, Height = CellHeight, Margin = new Padding(0), Padding = new Padding(0), TextAlign = ContentAlignment.MiddleCenter, BackColor = UserBackColor, ForeColor = UserForeColor, Text = $@"{col}{row}{subgrid}", Column = col, Row = row, SubGrid = SudokuPuzzle.GetRegion(col, row) }; SudokuPuzzle.SetAlternateRegionColors(ref sudokuLabel); sudokuLabels.Add(sudokuLabel); if (tableLayoutPanel1.Controls.Count < 81) { tableLayoutPanel1.Controls.Add(sudokuLabel); } } } }