C#Keydown或键码不起作用 [英] C# Keydown or keycode not working
本文介绍了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事件.
因此,即使在焦点放在任何其他控件上时,也要启用Form
的KeyDown
事件,必须将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 theKeyPreview
property ofForm
is set toFalse
by default. Let us say the cursor is in aTextBox
. Now, if theF3
key is pressed it will not go to theForm''s
KeyDown
event and instead it is captured by theTextBox
and the KeyDown event ofTextBox
fires.
So, to enable theKeyDown
event ofForm
even when the focus is on any other control, then theKeyPreview
property of theForm
has to be set totrue
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 settingKeyPreview
property the Form''sKeyDown
event will fire first, then theKeyDown
event of the control which has focus will be fired if theKeyPressEventArgs.Handled
is not set to true as stated above.
in the IntializeComponent() method set:
this.KeyPreview = true;
Regards,
Akaas Developer
Do checkuryour designer.cs (you can find this file underForm1.cs
ifForm1
is the name of the formu''veyou''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 indesigner.cs
file
Mark as solved ifu''veyou''ve got the solution
Thanks...
这篇关于C#Keydown或键码不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文