Click here to Skip to main content
15,880,725 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I saw in the internet the problem, and the solution was setting the "KeyPreview" to "true". I have tried it, and it still didn't work. Why when I press one of the arrows (down, up, left or right) the lable still doesn't show off?

Updated:
Thank you for your answer Peter Vegter, but It still doesn't work.
To make this clear: regular letter does work and makes the label show off. The thing is that the arrows (down, up, left or right) don't work.
I think I know why - the "focus" is on the the button and not on the form.
I searched for this in the internet, and I found that I need to make the "KeyPreview" set as "true". I did it, and still it doesn't show the label when I press the arrows, only when I press letters.
If i remove the button, the arrows do work.

What I have tried:

C#
public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            label1.Visible = false;
            this.KeyPreview = true;
            this.KeyDown += Form1_KeyDown;
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            label1.Visible = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }
    }
Posted
Updated 25-Oct-19 10:25am
v2
Comments
#realJSOP 26-Feb-18 13:11pm    
Set a breakpoint in the KeyDown event handler and see if the code is even handling the event.
[no name] 26-Feb-18 15:57pm    
My mistake, I've updated my answer. That should work.
[no name] 26-Oct-19 14:59pm    
I have tried "Solution 1" again with several buttons and labels on the Form and also with different .NET Frameworks and this solution still works well with me.
Maybe you made a mistake somewhere else in your code?

You have to use ProcessCmdKey to 'catch' the arrow (and some other) keys, like this:
C#
public Form1()
{
    InitializeComponent();
    label1.Visible = false;
}

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    switch (keyData)
    {
        case Keys.Left: // left arrow key
            label1.Visible = true;
            return true;

        case Keys.Right: // right arrow key
            label1.Visible = true;
            return true;

        // etc.
    }
    return base.ProcessCmdKey(ref msg, keyData);
}
 
Share this answer
 
v3
Comments
den2k88 27-Feb-18 3:33am    
I upvoted and bookmarked it: I will probably need it in the near future and I would have stumbled upon the same obstacle :D
[no name] 27-Feb-18 4:12am    
That's very nice, thank you!
Nelek 26-Oct-19 14:34pm    
OP has edited the question (yeah, it surprised me too, 1.5 years later) and he mentions you. You might have interest on checking up
[no name] 26-Oct-19 15:01pm    
Thanks Nelek!
I've tested the solution again and it still works (see answer above). And yes, a nice result after 1.5 years! :)
Richard Deeming 29-Oct-19 13:27pm    
Actually, the OP updated the question back in February 2018. The "updated" date on the question refers to the date when solution 2 was posted.

If you click on the "v2" link next to the date, you'll see the full revision history for the question[^]. :)
Place : this.Focus(); in your button after all the code the button does,

Problem is when button is clicked, it has the focus and your KeyDown and KeyUp events will not fire. Refocus on the form after your button is clicked and then your key events will once again fire.
 
Share this answer
 

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



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