如何使用按钮控制其他按钮的焦点?

How to use buttons to control the focus of other buttons?

我想用下面四个方向键来控制上面九个按钮的焦点,但是经过一番搜索,这个问题没有解决,所以来Whosebug希望得到解答。

感谢您的回答。原谅我是C#初学者

您可以将按钮 (1/2/3...A/S/D) 存储在二维数组中。按钮[3,3]。 然后将默认 Point 设置为 0,0(Point 是一个具有 2 个 int 的结构来标识位置)。

在 Up/Down 中点击 Decrease/Increase Y 坐标(决定你是否想做一个吃豆人)..即从最低的位置向下按到最高的位置等)

在Left/Right中点击Decrease/IncreaseX坐标。

每次点击后 - 从按钮集合中选择按钮,例如:

ButtonStore[Point.X, Point.Y]

然后调用设置焦点。 (可能是 Control.Focus() 之类的)。

private Button[,] btns = new Button[3,3] {
                           {button1,button2,button3},
                           {buttonQ,buttonW,buttonE},
                           {buttonA,buttonS,buttonD}};
private int x=0, y=0;

private void buttonLeft_Click(object sender, EventArgs e)
{
    if(y>0)
    {
        y--;
        btns[x,y].Focus();
    }
}

private void buttonRight_Click(object sender, EventArgs e)
{
    if(y<3)
    {
        y++;
        btns[x,y].Focus();
    }
}

private void buttonUp_Click(object sender, EventArgs e)
{
    if(x>0)
    {
        x--;
        btns[x,y].Focus();
    }
}

private void buttonDown_Click(object sender, EventArgs e)
{
    if(x<3)
    {
        x++;
        btns[x,y].Focus();
    }
}