Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Form can't handle KeyDown event!

Form can't handle KeyDown event!

Scheduled Pinned Locked Moved C#
csharpvisual-studiographicsdockerquestion
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    levgiang
    wrote on last edited by
    #1

    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

    C M 2 Replies Last reply
    0
    • L levgiang

      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

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • L levgiang

        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

        M Offline
        M Offline
        Mohamad Al Husseiny
        wrote on last edited by
        #3

        because when you add button control it has a focus when the form run so the message goes to button not to the form

        L 1 Reply Last reply
        0
        • M Mohamad Al Husseiny

          because when you add button control it has a focus when the form run so the message goes to button not to the form

          L Offline
          L Offline
          levgiang
          wrote on last edited by
          #4

          But when I set focus to Form : Form1.Focus() ; It still not runs. le van giang

          C 1 Reply Last reply
          0
          • L levgiang

            But when I set focus to Form : Form1.Focus() ; It still not runs. le van giang

            C Offline
            C Offline
            Christian Graus
            wrote on last edited by
            #5

            The property to set is on the Form, it's called KeyPreview. Set it to true. Christian Graus - Microsoft MVP - C++

            A 1 Reply Last reply
            0
            • C Christian Graus

              The property to set is on the Form, it's called KeyPreview. Set it to true. Christian Graus - Microsoft MVP - C++

              A Offline
              A Offline
              Anonymous
              wrote on last edited by
              #6

              Sorry,It can't run.You can try it.

              C 1 Reply Last reply
              0
              • A Anonymous

                Sorry,It can't run.You can try it.

                C Offline
                C Offline
                Christian Graus
                wrote on last edited by
                #7

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

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups