コード例 #1
0
        /// <summary>
        /// Generates a new textbox with the necessary event handlers
        /// </summary>
        private TextBox MakeTextBox(int row, int col)
        {
            // Make a new textbox
            TextBox newBox = new TabbityTextBox();

            newBox.BorderStyle = BorderStyle.None;
            newBox.Dock        = DockStyle.Fill;
            newBox.Multiline   = true; // Required to change height
            newBox.TextAlign   = HorizontalAlignment.Center;
            newBox.MaxLength   = 1;
            newBox.Margin      = new Padding(1);

            // When the box is focused
            newBox.Enter += (object sender, EventArgs e) =>
            {
                newBox.SelectAll();
                if (this.onSelect != null)
                {
                    this.onSelect(row, col);
                }
            };
            newBox.Click += (object sender, EventArgs e) => newBox.SelectAll();

            // When a key is pressed in the box
            newBox.KeyPress += (object sender, KeyPressEventArgs e) =>
            {
                // Only allow digits
                if ((!char.IsControl(e.KeyChar) &&
                     !char.IsDigit(e.KeyChar)) ||
                    e.KeyChar == '0')
                {
                    e.Handled = true;
                }
            };
            newBox.TextChanged += (object Sender, EventArgs e) =>
            {
                if (newBox.Text != "")
                {
                    SetCell(row, col, Int16.Parse(newBox.Text));
                    newBox.SelectAll();
                }
            };

            // Special keys change hte focus to other boxen
            newBox.KeyDown += (object sender, KeyEventArgs e) =>
            {
                switch (e.KeyData)
                {
                case Keys.Down:
                    FocusBox(row + 1, col);
                    e.SuppressKeyPress = true;
                    break;

                case Keys.Up:
                    FocusBox(row - 1, col);
                    e.SuppressKeyPress = true;
                    break;

                case Keys.Tab | Keys.Shift:
                case Keys.Left:
                    FocusBox(row, col - 1);
                    e.SuppressKeyPress = true;
                    break;

                case Keys.Tab:
                case Keys.Enter:
                case Keys.Right:
                    FocusBox(row, col + 1);
                    e.SuppressKeyPress = true;
                    break;

                case Keys.Back:
                case Keys.Space:
                case Keys.Delete:
                    ClearCell(row, col);
                    e.SuppressKeyPress = true;
                    break;
                }
            };

            return(newBox);
        }
コード例 #2
0
        /// <summary>
        /// Generates a new textbox with the necessary event handlers
        /// </summary>
        private TextBox MakeTextBox(int row, int col)
        {
            // Make a new textbox
            TextBox newBox = new TabbityTextBox();
            newBox.BorderStyle = BorderStyle.None;
            newBox.Dock = DockStyle.Fill;
            newBox.Multiline = true; // Required to change height
            newBox.TextAlign = HorizontalAlignment.Center;
            newBox.MaxLength = 1;
            newBox.Margin = new Padding(1);

            // When the box is focused
            newBox.Enter += (object sender, EventArgs e) =>
            {
                newBox.SelectAll();
                if (this.onSelect != null) this.onSelect(row, col);
            };
            newBox.Click += (object sender, EventArgs e) => newBox.SelectAll();

            // When a key is pressed in the box
            newBox.KeyPress += (object sender, KeyPressEventArgs e) =>
            {
                // Only allow digits
                if ((!char.IsControl(e.KeyChar)
                    && !char.IsDigit(e.KeyChar))
                    || e.KeyChar == '0')
                {
                    e.Handled = true;
                }
            };
            newBox.TextChanged += (object Sender, EventArgs e) =>
            {
                if (newBox.Text != "")
                {
                    SetCell(row, col, Int16.Parse(newBox.Text));
                    newBox.SelectAll();
                }
            };

            // Special keys change hte focus to other boxen
            newBox.KeyDown += (object sender, KeyEventArgs e) =>
            {
                switch (e.KeyData)
                {
                    case Keys.Down:
                        FocusBox(row + 1, col);
                        e.SuppressKeyPress = true;
                        break;
                    case Keys.Up:
                        FocusBox(row - 1, col);
                        e.SuppressKeyPress = true;
                        break;
                    case Keys.Tab | Keys.Shift:
                    case Keys.Left:
                        FocusBox(row, col-1);
                        e.SuppressKeyPress = true;
                        break;
                    case Keys.Tab:
                    case Keys.Enter:
                    case Keys.Right:
                        FocusBox(row, col+1);
                        e.SuppressKeyPress = true;
                        break;

                    case Keys.Back:
                    case Keys.Space:
                    case Keys.Delete:
                        ClearCell(row, col);
                        e.SuppressKeyPress = true;
                        break;
                }
            };

            return newBox;
        }