//make arrow keys move between text boxes for easier entry private void textBoxXY_KeyDown(object sender, KeyEventArgs e) { TextBoxXY tb = (TextBoxXY)sender; if (e.KeyValue == 37 || e.KeyValue == 38 || e.KeyValue == 39 || e.KeyValue == 40) { //don't let text box handle arrow key presses e.SuppressKeyPress = true; //handle left arrow if (e.KeyValue == 37 && tb.X > 0) { textBoxXY[tb.X - 1, tb.Y].Focus(); } //right arrow if (e.KeyValue == 39 && tb.X < 8) { textBoxXY[tb.X + 1, tb.Y].Focus(); } //up arrow if (e.KeyValue == 38 && tb.Y > 0) { textBoxXY[tb.X, tb.Y - 1].Focus(); } //down arrow if (e.KeyValue == 40 && tb.Y < 8) { textBoxXY[tb.X, tb.Y + 1].Focus(); } } return; }
private void textBoxXY_TextChanged(object sender, EventArgs e) { TextBoxXY curr = (TextBoxXY)sender; if (!curr.handleTextChange) { return; } String text = curr.Text; if (text.Length < 1) //test for text entered //TODO: redo this section with refreshDisplay { if (currentGrid[curr.X, curr.Y] == 0) //no text, no value, nothing to do. { return; } else //not text, but should be a known value { curr.Text = "" + currentGrid[curr.X, curr.Y]; //just reset text. No need to refreshDisplay return; } } //make sure a valid digit was entered. char digit = text[0]; if (!Char.IsDigit(digit) || digit == '0') { curr.Text = ""; //not a digit, delete text. No need to refreshDisplay return; } //see if just setting it to the value it is already known to be if (Char.GetNumericValue(digit) == currentGrid[curr.X, curr.Y]) { curr.Text = "" + currentGrid[curr.X, curr.Y]; //no change, just reset the text. No need to refreshDisplay return; } SudokuGrid undoTemp = new SudokuGrid(currentGrid); //attempt to apply the new digit if (!currentGrid.setKnownValue(curr.X, curr.Y, (int)Char.GetNumericValue(digit))) { curr.Text = ""; //change rejected, clear the text. No need to refreshDisplay return; } undoStack.Push(undoTemp); buttonUndo.Enabled = true; labelDebugInfo.Text = listPossibilities(curr.X, curr.Y); //force update possibilty list for this cell, since this cell has focus //change accepted, check entire grid, if autosolve is on if (checkBoxAuto.Checked) { currentGrid.solve(); } refreshDisplay(); }
private void textBoxXY_Leave(object sender, EventArgs e) { TextBoxXY tb = (TextBoxXY)sender; tb.handleTextChange = false; displayPossibilities(tb.X, tb.Y); labelDebugInfo.Text = ""; }
//display possibility info in labelMessage whenever textbox is entered private void textBoxXY_Enter(object sender, EventArgs e) { TextBoxXY tb = (TextBoxXY)sender; labelDebugInfo.Text = listPossibilities(tb.X, tb.Y); tb.handleTextChange = true; restoreFonts(tb); }
void InitializeComponentDynamic() { //dynamically initialize the components for the grid //creating the classic sudoku style by spacing the textboxes (with flat style outline) out over a black panel this.SuspendLayout(); const int smallMargin = 0; const int extraMargin = 2; //use the invisble dummy text box to get the correct text box height for the font size textBoxDummy.Font = new System.Drawing.Font("Microsoft Sans Serif", fontSize, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); textBoxDummy.Size = new System.Drawing.Size(1, 1); //setting it to 1x1 pixel, the textbox will clamp that value to smallest allowed for font size int textBoxSize = textBoxDummy.Size.Height; //get the clamped value to use in our real text boxes //create the text boxes textBoxXY = new TextBoxXY[9, 9]; for (int y = 0; y < 9; y++) { for (int x = 0; x < 9; x++) { //int boxIX = x % 3; //x index within group box it will be in (only matters for layout and assignments) //int boxIY = y % 3; //y " " " int boxX = smallMargin * (x + 1) + extraMargin * (1 + x / 3) + textBoxSize * x; //x coordinate within group box it will be in int boxY = smallMargin * (y + 1) + extraMargin * (1 + y / 3) + textBoxSize * y; //y " " " textBoxXY[x, y] = new TextBoxXY(x, y); textBoxXY[x, y].Location = new System.Drawing.Point(boxX, boxY); textBoxXY[x, y].MaxLength = 1; textBoxXY[x, y].Font = new System.Drawing.Font("Microsoft Sans Serif", fontSize, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); textBoxXY[x, y].Name = "textBox" + x.ToString() + y.ToString(); textBoxXY[x, y].Multiline = true; //allow multiline for Clutter (doesn't affect normal single character usage) (changes size when set for some odd reason?) textBoxXY[x, y].Size = new System.Drawing.Size(textBoxSize, textBoxSize); textBoxXY[x, y].TabIndex = y * 9 + x; textBoxXY[x, y].Text = ""; //textBoxXY[x, y].TabIndex.ToString(); textBoxXY[x, y].TextAlign = System.Windows.Forms.HorizontalAlignment.Center; //if(x < 5) textBoxXY[x, y].BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; textBoxXY[x, y].TextChanged += new System.EventHandler(this.textBoxXY_TextChanged); textBoxXY[x, y].Enter += new System.EventHandler(this.textBoxXY_Enter); textBoxXY[x, y].Leave += new System.EventHandler(this.textBoxXY_Leave); textBoxXY[x, y].KeyDown += new System.Windows.Forms.KeyEventHandler(this.textBoxXY_KeyDown); panel1.Controls.Add(textBoxXY[x, y]); } } int panelSize = 10 * smallMargin + 4 * extraMargin + 9 * textBoxSize; panel1.Size = new Size(panelSize, panelSize); panelDebugTools.Location = new Point(panelDebugTools.Location.X, panel1.Location.Y + panel1.Size.Height + panel1.Margin.Size.Height); panelDebugTools.Visible = checkBoxDebugTools.Checked; this.ResumeLayout(true); ResetSize(); }
//restore Font to normal size for number display/entry private void restoreFonts(TextBoxXY tb, bool force = false) { if (currentGrid[tb.X, tb.Y] == 0 || force) { Size size = tb.Size; tb.Text = ""; tb.MaxLength = 1; tb.Font = new System.Drawing.Font("Microsoft Sans Serif", fontSize, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); tb.Size = size; } }