为什么我的表单Key_down事件无法调用? [英] why my form Key_down event can not be invoked?
本文介绍了为什么我的表单Key_down事件无法调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
public partial class Form1:Form
private void InitializeComponent(){
// ... some控制....
//
// Form1
//
this .AutoScaleDimensions = new 系统.Drawing.SizeF(6F,12F);
this .AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this .ClientSize = new System.Drawing.Size( 498 , 298 );
this .Controls.Add( this .lwMRU_Projects);
this .Controls.Add( this .cbVS_Versions);
this .Controls.Add( this .lblVS_Versions);
this .Controls.Add( this .toolStrip1);
this .Controls.Add( this .statusStrip1);
this .MinimumSize = new System.Drawing.Size( 506 , 332 );
this .Name = Form1< /跨度>;
this .Text = Editor IV 跨度>;
this .Load + = new System.EventHandler(此跨度> .Form1_Load);
// ..................在这里! ................. //
此 .KeyDown + = new System.Windows.Forms.KeyEventHandler( this .key_down);
this .statusStrip1.ResumeLayout( false );
this .statusStrip1.PerformLayout();
this .toolStrip1.ResumeLayout( false );
this .toolStrip1.PerformLayout();
this .ResumeLayout( false );
this .PerformLayout();
// ----------无法始终调用------- ------- //
private void key_down(< span class =code-keyword> object sender,KeyEventArgs e)
{
}
}
但是 key_down 无法调用事件处理程序(我在开头放了一个断点)。任何提示?
解决方案
重点可能是表单中的控件,而不是表单本身。你可以将KeyPreview
属性设置为true
,如果焦点在控件上,你的key_down方法也会被调用形式如下:
此 .KeyPreview = true ;
可能是因为表单包含可以接受用户输入的控件 - 因此他们首先获取它,并且因为它们处理它所以它不会被传递到表单处理程序。
尝试设置 Form.KeyPreview Property [ ^ ]它应该开始工作。
我找到了解决方案!
我需要将表单的 KeyPreview 属性设置为True,以便识别按键事件!
public partial class Form1 : Form
private void InitializeComponent(){ //...some control.... // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(498, 298); this.Controls.Add(this.lwMRU_Projects); this.Controls.Add(this.cbVS_Versions); this.Controls.Add(this.lblVS_Versions); this.Controls.Add(this.toolStrip1); this.Controls.Add(this.statusStrip1); this.MinimumSize = new System.Drawing.Size(506, 332); this.Name = "Form1"; this.Text = "Editor IV"; this.Load += new System.EventHandler(this.Form1_Load); //..................here!.................// this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.key_down); this.statusStrip1.ResumeLayout(false); this.statusStrip1.PerformLayout(); this.toolStrip1.ResumeLayout(false); this.toolStrip1.PerformLayout(); this.ResumeLayout(false); this.PerformLayout(); //----------cannot invoked always--------------// private void key_down(object sender, KeyEventArgs e) { } }
but key_down event handler can not be called (I put a breakpoint at beginning). any hint?解决方案The focus is probably on a control in the form, not on the form itself. You can set theKeyPreview
property totrue
, then your key_down method will also get called if the focus is on a control in the form:
this.KeyPreview = true;
Probably because the form contains controls which can accept user input - so they get it first, and since they process it it isn't passed to the form handler.
Try setting the Form.KeyPreview Property[^] and it should start to work.
I have found the solution!
I need to set the KeyPreview Property of your Form to True so it will recognize the key press event!
这篇关于为什么我的表单Key_down事件无法调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文