简体   繁体   中英

Panel focus probleem

I'm trying to make a simple game: "Gomoku" or "Five in a Row" in a Windows.Form application. My program looks like this when is started and I don't press Tab key : 正常外观

But when I press Tab key it looks like this: 看错了

That board is a panel and every time I press Left, Right, Up, Down, A, D, W and S it's moving that two X from corners in the selected direction.

When I was presing Legt and Right it was switching tabPages so I added a textBox and I set focus to him:

if (textBox1.CanFocus) 
    textBox1.Focus();

设计外观

This is how I added background color and the lines on the board (game panel.

private void PuneFundal(Graphics g)
    {
        g.Clear(Color.FromArgb(150, 124, 92));
    }

private void DeseneazaLinie(Graphics g, Point p1, Point p2)
{
    Pen p = new Pen(Color.Black);
    g.DrawLine(p, p1, p2);
}

private void TrasareLinii(Graphics g)
{
    for (int i = 6, k = 0; k < 20; i += 16, k++)
        DeseneazaLinie(g, new Point(5, i), new Point(309, i));
    for (int i = 6, k = 0; k < 20; i += 16, k++)
        DeseneazaLinie(g, new Point(i, 5), new Point(i, 309));
}

private void Start_game()
{
    tabControl1.SelectedTab = tabPage2;
    Graphics g = game_panel.CreateGraphics();

    PuneFundal(g);
    TrasareLinii(g);
}

What can I do to work well when I press Tab key?

When you call tabControl1.SelectedTab = tabPage2; that means you set tabPage2 as the currently selected tab page, therefore it gains focus.

I recommend following solutions:

  1. You can always set focus of TextBox1 after selecting a tab page.
  2. Or use custom selectable panel which accepts focus. See @HansPassant answer here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM