Search code examples
c#buttoncontrolsfocus

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


I want to use the following four direction buttons to control the focus of the above nine buttons, but after a search, this problem has not been solved, so I come to stackoverflow and hope to get an answer.

Thank you for your answers. Forgive me for being a C# beginner.

enter image description here


Solution

  • 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();
        }
    }