Form can't handle KeyDown event!
-
When I create a application with a empty form ,I use followed function to handle KeyDown event : private void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) { switch (e.KeyCode) { case (Keys.Left) : MessageBox.Show("You have pressed Arrow-Key Left"); break; case (Keys.Right) : MessageBox.Show("You have pressed Arrow-Key Right"); break; } } The application runs well but when I add a new button , this function can't handle the KeyDown event ,this code no longer runs properly.Why ? I use Visual Studio .Net 2003. Sorry if my English is not good. This is all code : -First is Empty form(Run well) : namespace WindowsApplication2 { public class Form1 : System.Windows.Forms.Form { private System.ComponentModel.Container components = null; public Form1() { InitializeComponent(); } protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code private void InitializeComponent() { this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(292, 266); this.Name = "Form1"; this.Text = "Form1"; this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown); } #endregion [STAThread] static void Main() { Application.Run(new Form1()); } private void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) { switch (e.KeyCode) { case (Keys.Left) : MessageBox.Show("You have pressed Arrow-Key Left"); break; case (Keys.Right) : MessageBox.Show("You have pressed Arrow-Key Right"); break; } } } } - After add a button : namespace WindowsApplication2 { public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.Button button1; private System.ComponentModel.Container components = null; public Form1() { InitializeComponent(); } protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code private void InitializeComponent() { this.button1 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // button1 // th
-
When I create a application with a empty form ,I use followed function to handle KeyDown event : private void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) { switch (e.KeyCode) { case (Keys.Left) : MessageBox.Show("You have pressed Arrow-Key Left"); break; case (Keys.Right) : MessageBox.Show("You have pressed Arrow-Key Right"); break; } } The application runs well but when I add a new button , this function can't handle the KeyDown event ,this code no longer runs properly.Why ? I use Visual Studio .Net 2003. Sorry if my English is not good. This is all code : -First is Empty form(Run well) : namespace WindowsApplication2 { public class Form1 : System.Windows.Forms.Form { private System.ComponentModel.Container components = null; public Form1() { InitializeComponent(); } protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code private void InitializeComponent() { this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(292, 266); this.Name = "Form1"; this.Text = "Form1"; this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown); } #endregion [STAThread] static void Main() { Application.Run(new Form1()); } private void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) { switch (e.KeyCode) { case (Keys.Left) : MessageBox.Show("You have pressed Arrow-Key Left"); break; case (Keys.Right) : MessageBox.Show("You have pressed Arrow-Key Right"); break; } } } } - After add a button : namespace WindowsApplication2 { public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.Button button1; private System.ComponentModel.Container components = null; public Form1() { InitializeComponent(); } protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code private void InitializeComponent() { this.button1 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // button1 // th
You need to set a property on the button so that the keyboard event it recieves when it has the focus is passed to it's parent. Christian Graus - Microsoft MVP - C++
-
When I create a application with a empty form ,I use followed function to handle KeyDown event : private void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) { switch (e.KeyCode) { case (Keys.Left) : MessageBox.Show("You have pressed Arrow-Key Left"); break; case (Keys.Right) : MessageBox.Show("You have pressed Arrow-Key Right"); break; } } The application runs well but when I add a new button , this function can't handle the KeyDown event ,this code no longer runs properly.Why ? I use Visual Studio .Net 2003. Sorry if my English is not good. This is all code : -First is Empty form(Run well) : namespace WindowsApplication2 { public class Form1 : System.Windows.Forms.Form { private System.ComponentModel.Container components = null; public Form1() { InitializeComponent(); } protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code private void InitializeComponent() { this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(292, 266); this.Name = "Form1"; this.Text = "Form1"; this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown); } #endregion [STAThread] static void Main() { Application.Run(new Form1()); } private void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) { switch (e.KeyCode) { case (Keys.Left) : MessageBox.Show("You have pressed Arrow-Key Left"); break; case (Keys.Right) : MessageBox.Show("You have pressed Arrow-Key Right"); break; } } } } - After add a button : namespace WindowsApplication2 { public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.Button button1; private System.ComponentModel.Container components = null; public Form1() { InitializeComponent(); } protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code private void InitializeComponent() { this.button1 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // button1 // th
because when you add button control it has a focus when the form run so the message goes to button not to the form
-
because when you add button control it has a focus when the form run so the message goes to button not to the form
-
The property to set is on the Form, it's called KeyPreview. Set it to true. Christian Graus - Microsoft MVP - C++
-
The property to set is on the Form, it's called KeyPreview. Set it to true. Christian Graus - Microsoft MVP - C++
-
Fair enough. I'm sure that's what I did to solve it in my apps though. Is there a similar property on the buttons ? Christian Graus - Microsoft MVP - C++