0

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: Normal look

But when I press Tab key it looks like this: Wrong look

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

Design look

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?

apxcode
  • 7,696
  • 7
  • 30
  • 41
Tandura
  • 866
  • 2
  • 7
  • 19

1 Answers1

0

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.
Community
  • 1
  • 1
Hassan
  • 5,360
  • 2
  • 22
  • 35