Click here to Skip to main content
15,882,464 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have this form and on formMain's KeyDown event it detects the arrows to play the board game, but when I press the Restart button which I made a panel for to get rid of it taking focus when I start the game, now the game starts correctly as KeyDown event for the form fires as it has focus when the button Restart is in a panel, but when I click restart or it gets focus it cant lose that focus even when I press on the form outside the panel so I be able to play the game with the arrows with the formMain's KeyDown, when the button restart gets the control I can't make it lose it even with tabStop=false;

I want the form to get focus so it detect the KeyDown event, I can fix it with PreviewKeyDown but I want a legit solution.

https://prnt.sc/SAx3YnbLr_hm[^]

What I have tried:

private void panel1_Enter(object sender, EventArgs e)
        {
            Focus();
            Select();
        }

 Graphics g;
        private void FormMain_Shown(object sender, EventArgs e)
        {
            g = CreateGraphics();
            drawAll();


            

            buttonRestart.TabStop = false;

            ActiveControl = label1;// hidden label as its Visible=false


            Focus();
            Select();
        }

        private void buttonRestart_Enter(object sender, EventArgs e)
        {
            Focus();
            Select();
        }
Posted
Updated 24-Apr-23 23:35pm
v2

You don't - if there is a control on the form that accepts input (and Buttons accept some inputs) then it has thee focus, and the form doesn't get them.

You can override this using the Form.KeyPreview Property (System.Windows.Forms) | Microsoft Learn[^] in conjunction with the Form.ProcessCmdKey(Message, Keys) Method (System.Windows.Forms) | Microsoft Learn[^] override.
 
Share this answer
 
In your form load, you set the ActiveControl to, according to your comment, a hidden label label1. That overrides the default focus and, I'm guessing, gives you the arrow key behavior you want. Have you tried setting ActiveControl in your handler for the Reset button?
 
Share this answer
 
Comments
John Smith 27 25-Apr-23 5:37am    
This prevents the formMains's KeyDown from firing the arrows down:

private void buttonRestart_Enter(object sender, EventArgs e)
{
ActiveControl = label1;
}

even when label1.Visible = true;
This worked like magic, thx for your answers anyway:


private void buttonRestart_Enter(object sender, EventArgs e)
{
    ActiveControl = null;
}


buttonRestart is inside a panel in the form by the way... Edit: It can also be in the form without a panel for it to be in.
 
Share this answer
 
v3
Comments
Dave Kreskowiak 25-Apr-23 9:55am    
There are right ways to handle non-specific control input and wrong ways to do it. This is a wrong way. It's not robust at all and will fail on you, or worse yet, your customers.

Griff has the correct way.
John Smith 27 25-Apr-23 10:20am    
Sorry, I'm only a hobbyist and this seems complicated for me...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900