C#键侦听

我正在构建一个数独游戏,我想听一个数字键并将其应用于当前选择的多维数据集。 但是,由于某些原因,按键不起作用。

我的代码:

 namespace SodukoApp
    {
    public partial class SudokuFrame : UserControl
    {
        // Data
        private const int buttonWidth = 34;
        private const int buttonHeight = 34;
        private const int spaceBetweenButtons = 5;
        private const int rowAmount = 9;
        private const int colAmount = 9;
        private List<List<Button>> buttons;
        private int numOfZeroes;
        private int[,] currBoard;
        private bool haveSolution;
        private Button pressed;

        public SudokuFrame()
        {
            this.buttons = new List<List<Button>>();
            this.numOfZeroes = 62;
            InitializeComponent();
        }

        private void SudokuFrame_Load(object sender, EventArgs e)
        {
            this.difficulty.SelectedIndex = 0;
            this.buildABoard();
            this.generateBoardAndPrindToScreen();
        }

        public void SudokuFrame_KeyPress(object sender, KeyPressEventArgs e)
        {
            System.Console.WriteLine("somthing");
        }

        private void gameBoardButton_Click(object sender, EventArgs e)
        {
            if(this.pressed != null)
                this.pressed.FlatAppearance.BorderSize = 0;
            this.pressed = (Button)sender;
            this.pressed.FlatAppearance.BorderSize = 1;
        }
    }
评论
大白痴
大白痴

请参阅下面的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SodukoApp
{
    public partial class SudokuFrame : UserControl
    {
        // Data
        private const int buttonWidth = 34;
        private const int buttonHeight = 34;
        private const int spaceBetweenButtons = 5;
        private const int rowAmount = 9;
        private const int colAmount = 9;
        private List<List<MyButton>> buttons;
        private int numOfZeroes;
        private int[,] currBoard;
        private bool haveSolution;
        private MyButton pressed;


        public SudokuFrame()
        {
            this.buttons = new List<List<MyButton>>();
            this.numOfZeroes = 62;
            InitializeComponent();

            this.Load += new EventHandler(SudokuFrame_Load);
        }

        private void SudokuFrame_Load(object sender, EventArgs e)
        {
            difficulty.Items.Add(1);
            this.difficulty.SelectedIndex = 0;
            this.Height = rowAmount * (spaceBetweenButtons * buttonHeight) + 10;
            this.Width = colAmount * (spaceBetweenButtons * buttonWidth) + 10;
            this.buildABoard();
            this.generateBoardAndPrindToScreen();
            this.BringToFront();
        }

        public void SudokuFrame_KeyPress(object sender, KeyPressEventArgs e)
        {
            System.Console.WriteLine("somthing");
        }

        private void gameBoardButton_Click(object sender, EventArgs e)
        {
            MyButton myButton = sender as MyButton;
            int row = myButton.row;
            int col = myButton.col;
            if (this.pressed != null)
            {
                this.pressed.FlatAppearance.BorderSize = 0;
                this.pressed.BackColor = (pressed.row / 3 + pressed.col / 3) % 2 == 0 ? Color.Red : Color.Black;
            }
            this.pressed = myButton;
            this.pressed.BackColor = Color.White;
            this.pressed.FlatAppearance.BorderSize = 1;
        }
        private void buildABoard()
        {
            for (int row = 0; row < rowAmount; row++)
            {
                List<MyButton> newRow = new List<MyButton>();
                buttons.Add(newRow);
                for (int col = 0; col < colAmount; col++)
                {
                    MyButton newButton = new MyButton();
                    newButton.Width = buttonWidth;
                    newButton.Height = buttonHeight;
                    newButton.Top = row * (buttonHeight + spaceBetweenButtons);
                    newButton.Left = col * (buttonWidth + spaceBetweenButtons);
                    newButton.BackColor = (row / 3 + col / 3) % 2 == 0 ? Color.Red : Color.Black;
                    newButton.Click += new EventHandler(gameBoardButton_Click);
                    newButton.row = row;
                    newButton.col = col;
                    newRow.Add(newButton);
                    this.Controls.Add(newButton);
                }
            }
        }
        private void generateBoardAndPrindToScreen()
        {
        }
    }
    public class MyButton : Button
    {
        public int row { get; set; }
        public int col { get; set; }
    }

}
点赞
评论