Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
this is my code

C#
private void Form1_KeyDown(object sender, KeyEventArgs e)
     {
if (e.KeyCode == Keys.F3)
         {
             Label intervillabel = new Label();
             intervillabel.Text = numericUpDown1.Value.ToString();
             intervillabel.Location = new Point(BeatDetector.Location.X, 10);
             intervillabel.Visible = true;

             panel1.Controls.Add(intervillabel);
         }
     }


the error is, nothing happens,l ive tried every key
Posted
Updated 26-Nov-22 9:31am

The reason is that when a key is pressed it will go to the control which has focus on the form, as the KeyPreview property of Form is set to False by default. Let us say the cursor is in a TextBox. Now, if the F3 key is pressed it will not go to the Form's KeyDown event and instead it is captured by the TextBox and the KeyDown event of TextBox fires.

So, to enable the KeyDown event of Form even when the focus is on any other control, then the KeyPreview property of the Form has to be set to true as explained here
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.keypreview.aspx[^]
KeyPreview Gets or sets a value indicating whether the form will receive key events before the event is passed to the control that has focus.
and under remarks as
To handle keyboard events only at the form level and not allow controls to receive keyboard events, set the KeyPressEventArgs.Handled property in your form's KeyPress event handler to true
.

After setting KeyPreview property the Form's KeyDown event will fire first, then the KeyDown event of the control which has focus will be fired if the KeyPressEventArgs.Handled is not set to true as stated above.
 
Share this answer
 
v2
Comments
Shahin Khorshidnia 2-May-12 8:54am    
This is one the most perfect answers
+6
VJ Reddy 2-May-12 9:36am    
Thank you very much, Shahin.
YDaoust 12-Sep-22 4:57am    
Worked for me, thanks.
in the IntializeComponent() method set:
this.KeyPreview = true;

Regards,
Akaas Developer
 
Share this answer
 
v2
Comments
Pete O'Hanlon 1-May-12 6:31am    
Please remove the link - if you do not, I will have no choice but to have you removed as a spammer, and remove all your answers. As it looks like you are genuinely trying to answer here, I don't want to go with this option, but I will do so if I have to.
Member 12101808 19-Dec-15 10:01am    
what a joke!! that guy tried to be nice and provide a resource and you want to threaten him with being marked as a spammer WOW. talk about being a scared cat. why don't you get on to users who should be marked as spammers and leave the good people alone. Oh go ahead give me a warning it wont bother me I sure wont loose any sleep over this stupid site
Pete O'Hanlon 19-Dec-15 10:44am    
If you look, you'll see that this answer has been edited. His or her first answer included a spam link pointing to some completely unrelated item. If you look here you can see the original answer: http://www.codeproject.com/script/Answers/EntryVersion.aspx?aid=376438&av=550454
Do check ur your designer.cs (you can find this file under Form1.cs if Form1 is the name of the form u've you've given) file and check whether the eventhandler is associated with this event or not!!!
Because to fire an event there must be handler for it which is associted with the event in designer.cs file

Mark as solved if u've you've got the solution
Thanks...
 
Share this answer
 
v2
Comments
Shahin Khorshidnia 2-May-12 9:03am    
It's very nice that you want to help. Thank you.
But, Please do not use sms words like "u", "ur", "u've" and... You have enough time to type them like "you", "your", "you've".
And if I were you I'd use bold for titles and a very important word not for the whole of the answer. (Thank you)
preet88 4-May-12 1:28am    
thanks for your suggestions.
Shahin Khorshidnia 4-May-12 5:17am    
You're welcome :)
BillW33 2-May-12 9:11am    
@Shahin, +5 for your comment. :)
Shahin Khorshidnia 4-May-12 5:16am    
Thank you :)

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