C#Keydown或键码不起作用 [英] C# Keydown or keycode not working

查看:449
本文介绍了C#Keydown或键码不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码

 私有  void  Form1_KeyDown(对象发​​件人,KeyEventArgs e)
     {
如果(例如,KeyCode == Keys.F3)
         {
             标签intervillabel =  Label();
             intervillabel.Text = numericUpDown1.Value.ToString();
             intervillabel.Location = 点(BeatDetector.Location.X, 10 );
             intervillabel.Visible =  true ;

             panel1.Controls.Add(intervillabel);
         }
     } 



错误是没有任何反应,我尝试了每个键.

解决方案

原因是,当按下某个键时,它将转到以该窗体为焦点的控件,因为Form KeyPreview 属性默认设置为False .假设光标位于TextBox中.现在,如果按下F3 键,它将不会进入Form''s KeyDown事件,而是由TextBox 捕获,并且触发TextBox 的KeyDown事件.

因此,即使在焦点放在任何其他控件上时,也要启用FormKeyDown事件,必须将Form KeyPreview 属性设置为true ,如此处所述
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.keypreview.aspx [ ^ ]
KeyPreview获取或设置一个值,该值指示在将事件传递给具有焦点的控件之前,表单是否将接收键事件.
并注明为
若要仅在窗体级别处理键盘事件,并且不允许控件接收键盘事件,请将窗体的KeyPress事件处理程序中的KeyPressEventArgs.Handled属性设置为true
.

设置KeyPreview 属性后,将首先触发Form的KeyDown事件,然后,如果KeyPressEventArgs.Handled的设置没有如上所述,则具有焦点的控件的KeyDown事件将被触发.

设置为:
this.KeyPreview = true;

问候,
Akaas Developer


检查您的designer.cs(如果Form1是格式的名称,您可以在Form1.cs下找到此文件) u 您已给出)文件,并检查事件处理程序是否与此事件相关联!!!
因为要触发一个事件,所以必须在designer.cs文件中为该事件关联该事件的处理程序

如果 u 您已找到解决方案,则标记为已解决
谢谢...


this is my code

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

解决方案

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.


in the IntializeComponent() method set:
this.KeyPreview = true;

Regards,
Akaas Developer


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...


这篇关于C#Keydown或键码不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆