public void GenerateGrid(int[] originalNumbersArray, int[] currentSaveArray) { for (var i = 0; i < originalNumbersArray.Length; i++) { if (originalNumbersArray[i] == 0) { if (currentSaveArray[i] != 0) { sudokuPanel.Controls.Add(controller.MakeSudokuButton("sudoku_", currentSaveArray[i].ToString(), game.GetRowByIndex(i), game.GetColumnByIndex(i), false)); } else { sudokuPanel.Controls.Add(controller.MakeSudokuButton("sudoku_", "", game.GetRowByIndex(i), game.GetColumnByIndex(i), false)); } } else { sudokuPanel.Controls.Add(controller.MakeSudokuButton("sudoku_", originalNumbersArray[i].ToString(), game.GetRowByIndex(i), game.GetColumnByIndex(i), true)); } } // Giving all of the textboxes the commmon event + add the borders foreach (Control ctrl in sudokuPanel.Controls) { if ((ctrl as TextBox) != null) { (ctrl as TextBox).TextChanged += CommonHandler_TextChanged; } string[] splitArray = (ctrl as TextBox).Name.Split('_'); int currentIndex = game.GetByColumn(int.Parse(splitArray[1]), int.Parse(splitArray[2])); if ((game.GetRowByIndex(currentIndex) + 1) % (game.gridHeight / game.squareHeight) == 0) // it is on a row with a horizonal line, bottom { (ctrl as TextBox).Controls.Add(new Label() { Height = 3, Dock = DockStyle.Bottom, BackColor = Color.Black }); } if ((game.GetColumnByIndex(currentIndex) + 1) % (game.gridWidth / game.squareWidth) - 1 == 0 && game.GetColumnByIndex(currentIndex) != 0) { (ctrl as TextBox).Controls.Add(new Label() { Width = 3, Dock = DockStyle.Left, BackColor = Color.Black }); } } }