MH-SudokuSolver ver3 Implementation Issues

In version 3 of MH-SudokuSolver, the main issue addressed is of navigation.

In the previous versions, each cell was of the type MaskedTextBox. For a MaskedTextBox, we have to specify a prompt character. This is the character that appears in the box by default. I had chosen space as the prompt character.

When you ran the program, the cursor was available in the first box. You either enter a digit or not. Navigation was through the tab key. As you kept tabbing, you navigated left to right and up - down each cell, and entered a digit in the right cell. However, if you went ahead and clicked the mouse in any cell directly, you weren't able to enter a digit, unless you pressed the backspace. This was a bit awkward, as you would see a cell without any entry, but werent' allowed to enter any digit because of the invisible space character and it is not so intuitive to press the backspace key.

Initially in the first version, I didn't use the TextBox type for the cells, because I thought I had to write event handler for each of the cells, so I had to repeat the same kind of code 81 times. What was I thinking? I recently realized that I had to write the event handler once and hook (register in the Java world) all 81 textboxes to the same event handler.

It took a little bit of googling around to figure out what was the correct event to handle and once I figured out it was the KeyPress, the event handler code to allow only the digits 1 -9 and the backspace key was easily written as:

private void textBox_KeyPress(object sender, KeyPressEventArgs e)

{

    if (e.KeyChar != '1' &&

       e.KeyChar != '2' &&

       e.KeyChar != '3' &&

       e.KeyChar != '4' &&

       e.KeyChar != '5' &&

       e.KeyChar != '6' &&

       e.KeyChar != '7' &&

       e.KeyChar != '8' &&

       e.KeyChar != '9' &&

       e.KeyChar != '\b')

       e.Handled = true;

}

All 81 cells have their KeyPress event hooked to this handler as follows:

textBox1.KeyPress += new KeyPressEventHandler(textBox_KeyPress);

textBox2.KeyPress += new KeyPressEventHandler(textBox_KeyPress);

..........................

.........................

textBox80.KeyPress += new KeyPressEventHandler(textBox_KeyPress);

textBox81.KeyPress += new KeyPressEventHandler(textBox_KeyPress);

Now the MH-SudokuSolver program allows you to mouse-click in any cell and enter a digit between 1 and 9 directly without having to press the backspace key.


Back          Home

Comments