public TextBox[,] GetTwoDimensionalTextBoxArray(Form form) { TextBox[] array = GetOneDimensionalTextBoxArray(form); TextBox[,] array2d = new TextBox[9, 9]; int arrayCount = 0; for (int row = 0; row < 9; row++) { for (int column = 0; column < 9; column++) { if (arrayCount < 81) { int boxIndex = GetBoxIndex(row, column); array[arrayCount].Tag = new SudokuTextBox() { Row = row, Column = column, BoxIndex = boxIndex }; array2d[row, column] = array[arrayCount]; array2d[row, column].TextChanged += (sender, EventArgs) => { if (handlersOn) { TextBox textbox = sender as TextBox; SudokuTextBox information = textbox.Tag as SudokuTextBox; bool passed = CheckEntry(information.Row, information.Column); if (!passed) { SudokuGrid[information.Row, information.Column].ForeColor = Color.Red; } else { SudokuGrid[information.Row, information.Column].ForeColor = Color.Black; } } }; ++arrayCount; } } } return(array2d); }
public bool CheckBox(int row, int column) { if (handlersOn) { TextBox source = SudokuGrid[row, column]; SudokuTextBox information = source.Tag as SudokuTextBox; switch (information.BoxIndex) { case 0: for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { TextBox textbox = SudokuGrid[i, j]; if (row != i || column != j) { if (source.Text == textbox.Text && !string.IsNullOrWhiteSpace(source.Text)) { return(false); } } } } break; case 1: for (int i = 0; i < 3; i++) { for (int j = 3; j < 6; j++) { TextBox textbox = SudokuGrid[i, j]; if (row != i || column != j) { if (source.Text == textbox.Text && !string.IsNullOrWhiteSpace(source.Text)) { return(false); } } } } break; case 2: for (int i = 0; i < 3; i++) { for (int j = 6; j < 9; j++) { TextBox textbox = SudokuGrid[i, j]; if (row != i || column != j) { if (source.Text == textbox.Text && !string.IsNullOrWhiteSpace(source.Text)) { return(false); } } } } break; case 3: for (int i = 3; i < 6; i++) { for (int j = 0; j < 3; j++) { TextBox textbox = SudokuGrid[i, j]; if (row != i || column != j) { if (source.Text == textbox.Text && !string.IsNullOrWhiteSpace(source.Text)) { return(false); } } } } break; case 4: for (int i = 3; i < 6; i++) { for (int j = 3; j < 6; j++) { TextBox textbox = SudokuGrid[i, j]; if (row != i || column != j) { if (source.Text == textbox.Text && !string.IsNullOrWhiteSpace(source.Text)) { return(false); } } } } break; case 5: for (int i = 3; i < 6; i++) { for (int j = 6; j < 9; j++) { TextBox textbox = SudokuGrid[i, j]; if (row != i || column != j) { if (source.Text == textbox.Text && !string.IsNullOrWhiteSpace(source.Text)) { return(false); } } } } break; case 6: for (int i = 6; i < 9; i++) { for (int j = 0; j < 3; j++) { TextBox textbox = SudokuGrid[i, j]; if (row != i || column != j) { if (source.Text == textbox.Text && !string.IsNullOrWhiteSpace(source.Text)) { return(false); } } } } break; case 7: for (int i = 6; i < 9; i++) { for (int j = 3; j < 6; j++) { TextBox textbox = SudokuGrid[i, j]; if (row != i || column != j) { if (source.Text == textbox.Text && !string.IsNullOrWhiteSpace(source.Text)) { return(false); } } } } break; case 8: for (int i = 6; i < 9; i++) { for (int j = 6; j < 9; j++) { TextBox textbox = SudokuGrid[i, j]; if (row != i || column != j) { if (source.Text == textbox.Text && !string.IsNullOrWhiteSpace(source.Text)) { return(false); } } } } break; default: return(false); } return(true); } else { string source = SolvedSudokuGrid[row, column]; SudokuTextBox information = SudokuGrid[row, column].Tag as SudokuTextBox; switch (information.BoxIndex) { case 0: for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { string currentText = SolvedSudokuGrid[i, j]; if (row != i || column != j) { if (source == currentText && !string.IsNullOrWhiteSpace(source)) { return(false); } } } } break; case 1: for (int i = 0; i < 3; i++) { for (int j = 3; j < 6; j++) { string currentText = SolvedSudokuGrid[i, j]; if (row != i || column != j) { if (source == currentText && !string.IsNullOrWhiteSpace(source)) { return(false); } } } } break; case 2: for (int i = 0; i < 3; i++) { for (int j = 6; j < 9; j++) { string currentText = SolvedSudokuGrid[i, j]; if (row != i || column != j) { if (source == currentText && !string.IsNullOrWhiteSpace(source)) { return(false); } } } } break; case 3: for (int i = 3; i < 6; i++) { for (int j = 0; j < 3; j++) { string currentText = SolvedSudokuGrid[i, j]; if (row != i || column != j) { if (source == currentText && !string.IsNullOrWhiteSpace(source)) { return(false); } } } } break; case 4: for (int i = 3; i < 6; i++) { for (int j = 3; j < 6; j++) { string currentText = SolvedSudokuGrid[i, j]; if (row != i || column != j) { if (source == currentText && !string.IsNullOrWhiteSpace(source)) { return(false); } } } } break; case 5: for (int i = 3; i < 6; i++) { for (int j = 6; j < 9; j++) { string currentText = SolvedSudokuGrid[i, j]; if (row != i || column != j) { if (source == currentText && !string.IsNullOrWhiteSpace(source)) { return(false); } } } } break; case 6: for (int i = 6; i < 9; i++) { for (int j = 0; j < 3; j++) { string currentText = SolvedSudokuGrid[i, j]; if (row != i || column != j) { if (source == currentText && !string.IsNullOrWhiteSpace(source)) { return(false); } } } } break; case 7: for (int i = 6; i < 9; i++) { for (int j = 3; j < 6; j++) { string currentText = SolvedSudokuGrid[i, j]; if (row != i || column != j) { if (source == currentText && !string.IsNullOrWhiteSpace(source)) { return(false); } } } } break; case 8: for (int i = 6; i < 9; i++) { for (int j = 6; j < 9; j++) { string currentText = SolvedSudokuGrid[i, j]; if (row != i || column != j) { if (source == currentText && !string.IsNullOrWhiteSpace(source)) { return(false); } } } } break; default: return(false); } return(true); } }