CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2012
    Posts
    15

    keydown event, not working.

    I'm trying to move this button in the x axis.. I used the following code.
    Code:
    private: System::Void Form1_KeyDown(System::Object^  sender, System::Windows::Forms::KeyEventArgs^  e) {
    				   if( e->KeyCode==Keys::Up)
    				 {
    					 ++x;
    					 label1->Text="up ";
    					 button1->Location = Point(x, y);
    					 
    				 }
    But nothing happens... But when i try the same thing after putting a textbox, it works. Heres the code.
    Code:
    private: System::Void textBox1_KeyDown(System::Object^  sender, System::Windows::Forms::KeyEventArgs^  e) {
    				 if( e->KeyCode==Keys::Up)
    				 {
    					 ++x;
    					 label1->Text="up ";
    					 button1->Location = Point(x, y);
    					
    				 }
    Any ideas?

  2. #2
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: keydown event, not working.

    Looks like the focused control (the button, most probably) consumes the keystrokes before the form gets notice of them. Try setting the form's KeyPreview property to true.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  3. #3
    Join Date
    Jun 2012
    Posts
    15

    Re: keydown event, not working.

    Thanks . will check it out.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured