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. Managed C++/CLI
  4. Event on KeyPress

Event on KeyPress

Scheduled Pinned Locked Moved Managed C++/CLI
helpquestion
12 Posts 3 Posters 2 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.
  • P Offline
    P Offline
    Polar_Sheep
    wrote on last edited by
    #1

    Hi guys, I have a problem with event on key press enter. I have a TextBox1, then I am a typing to him and I need to if KeyPress was ENTER call my function Result(). I think I can not do something function like that: void textBox1_KeyPress( Object^ /*sender*/, System::Windows::Forms::KeyPressEventArgs^ e ) { if(e->KeyCode == Key::Enter) // if press key is enter Result(); // my function } I am looking for some pieces of code in msdn, but I still have a problem somewhere. KeyPreview in Properties Form1 is true. I am so sorry for this stupid question :(

    M 1 Reply Last reply
    0
    • P Polar_Sheep

      Hi guys, I have a problem with event on key press enter. I have a TextBox1, then I am a typing to him and I need to if KeyPress was ENTER call my function Result(). I think I can not do something function like that: void textBox1_KeyPress( Object^ /*sender*/, System::Windows::Forms::KeyPressEventArgs^ e ) { if(e->KeyCode == Key::Enter) // if press key is enter Result(); // my function } I am looking for some pieces of code in msdn, but I still have a problem somewhere. KeyPreview in Properties Form1 is true. I am so sorry for this stupid question :(

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      From the docs: "The KeyPress event is not raised by noncharacter keys; however, the noncharacter keys do raise the KeyDown and KeyUp events."

      Mark Salsbery Microsoft MVP - Visual C++ :java:

      P 1 Reply Last reply
      0
      • M Mark Salsbery

        From the docs: "The KeyPress event is not raised by noncharacter keys; however, the noncharacter keys do raise the KeyDown and KeyUp events."

        Mark Salsbery Microsoft MVP - Visual C++ :java:

        P Offline
        P Offline
        Polar_Sheep
        wrote on last edited by
        #3

        Thanks, but I still not work. I have this: void textBox1_KeyDown( Object^ /*sender*/, System::Windows::Forms::KeyEventArgs^ e ) { if ( e->KeyCode == Keys::Enter) textBox2->Text = "Enter was pressed"; }

        M 1 Reply Last reply
        0
        • P Polar_Sheep

          Thanks, but I still not work. I have this: void textBox1_KeyDown( Object^ /*sender*/, System::Windows::Forms::KeyEventArgs^ e ) { if ( e->KeyCode == Keys::Enter) textBox2->Text = "Enter was pressed"; }

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

          Polar_Sheep wrote:

          I still not work

          Maybe try setting the TextBox's AcceptsReturn property to true. Mark

          Mark Salsbery Microsoft MVP - Visual C++ :java:

          P 1 Reply Last reply
          0
          • M Mark Salsbery

            Polar_Sheep wrote:

            I still not work

            Maybe try setting the TextBox's AcceptsReturn property to true. Mark

            Mark Salsbery Microsoft MVP - Visual C++ :java:

            P Offline
            P Offline
            Polar_Sheep
            wrote on last edited by
            #5

            AcceptsReturn True... Still is not work. Shall I have some code in Initialize component? I mean something like this: this->textBox1->KeyDown = gcnew... something?

            M 1 Reply Last reply
            0
            • P Polar_Sheep

              AcceptsReturn True... Still is not work. Shall I have some code in Initialize component? I mean something like this: this->textBox1->KeyDown = gcnew... something?

              M Offline
              M Offline
              Mark Salsbery
              wrote on last edited by
              #6

              Yes you need to create the delegate and add it to the event. Can you show that code? Mark

              Mark Salsbery Microsoft MVP - Visual C++ :java:

              P D 2 Replies Last reply
              0
              • M Mark Salsbery

                Yes you need to create the delegate and add it to the event. Can you show that code? Mark

                Mark Salsbery Microsoft MVP - Visual C++ :java:

                P Offline
                P Offline
                Polar_Sheep
                wrote on last edited by
                #7

                #pragma once namespace PoHavarii { using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing; public ref class Form1 : public System::Windows::Forms::Form { public: Form1(void) { InitializeComponent(); } protected: ~Form1() { if (components) { delete components; } } private: System::Windows::Forms::TextBox^ textBox1; protected: private: System::Windows::Forms::TextBox^ textBox2; private: System::ComponentModel::Container ^components; #pragma region Windows Form Designer generated code void InitializeComponent(void) { this->textBox1 = (gcnew System::Windows::Forms::TextBox()); this->textBox2 = (gcnew System::Windows::Forms::TextBox()); this->SuspendLayout(); // // textBox1 // this->textBox1->AcceptsReturn = true; this->textBox1->Location = System::Drawing::Point(108, 55); this->textBox1->Name = L"textBox1"; this->textBox1->Size = System::Drawing::Size(181, 20); this->textBox1->TabIndex = 0; // // textBox2 // this->textBox2->Location = System::Drawing::Point(108, 94); this->textBox2->Name = L"textBox2"; this->textBox2->Size = System::Drawing::Size(181, 20); this->textBox2->TabIndex = 1; // // Form1 // this->AutoScaleDimensions = System::Drawing::SizeF(6, 13); this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; this->ClientSize = System::Drawing::Size(391, 264); this->Controls->Add(this->textBox2); this->Controls->Add(this->textBox1); this->KeyPreview = true; this->Name = L"Form1"; this->Text = L"Form1"; this->ResumeLayout(false); this->PerformLayout(); } #pragma endregion void textBox1_KeyDown( Object^ /*sender*/, System::Windows::Forms::KeyEventArgs^ e ) { if ( e->KeyCode == Keys::Enter) textBox2->Text = L"Enter was pressed"; } }; }

                M 1 Reply Last reply
                0
                • P Polar_Sheep

                  #pragma once namespace PoHavarii { using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing; public ref class Form1 : public System::Windows::Forms::Form { public: Form1(void) { InitializeComponent(); } protected: ~Form1() { if (components) { delete components; } } private: System::Windows::Forms::TextBox^ textBox1; protected: private: System::Windows::Forms::TextBox^ textBox2; private: System::ComponentModel::Container ^components; #pragma region Windows Form Designer generated code void InitializeComponent(void) { this->textBox1 = (gcnew System::Windows::Forms::TextBox()); this->textBox2 = (gcnew System::Windows::Forms::TextBox()); this->SuspendLayout(); // // textBox1 // this->textBox1->AcceptsReturn = true; this->textBox1->Location = System::Drawing::Point(108, 55); this->textBox1->Name = L"textBox1"; this->textBox1->Size = System::Drawing::Size(181, 20); this->textBox1->TabIndex = 0; // // textBox2 // this->textBox2->Location = System::Drawing::Point(108, 94); this->textBox2->Name = L"textBox2"; this->textBox2->Size = System::Drawing::Size(181, 20); this->textBox2->TabIndex = 1; // // Form1 // this->AutoScaleDimensions = System::Drawing::SizeF(6, 13); this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; this->ClientSize = System::Drawing::Size(391, 264); this->Controls->Add(this->textBox2); this->Controls->Add(this->textBox1); this->KeyPreview = true; this->Name = L"Form1"; this->Text = L"Form1"; this->ResumeLayout(false); this->PerformLayout(); } #pragma endregion void textBox1_KeyDown( Object^ /*sender*/, System::Windows::Forms::KeyEventArgs^ e ) { if ( e->KeyCode == Keys::Enter) textBox2->Text = L"Enter was pressed"; } }; }

                  M Offline
                  M Offline
                  Mark Salsbery
                  wrote on last edited by
                  #8

                  In initializeComponent(), create a delegate and add it to textBox1's KeyDown event:

                  this->textBox1->KeyDown += gcnew System::KeyEventHandler(this, &Form1::textBox1_KeyDown);

                  Mark *edit* Fixed code in bold

                  Mark Salsbery Microsoft MVP - Visual C++ :java:

                  modified on Saturday, July 26, 2008 4:35 PM

                  P 1 Reply Last reply
                  0
                  • M Mark Salsbery

                    In initializeComponent(), create a delegate and add it to textBox1's KeyDown event:

                    this->textBox1->KeyDown += gcnew System::KeyEventHandler(this, &Form1::textBox1_KeyDown);

                    Mark *edit* Fixed code in bold

                    Mark Salsbery Microsoft MVP - Visual C++ :java:

                    modified on Saturday, July 26, 2008 4:35 PM

                    P Offline
                    P Offline
                    Polar_Sheep
                    wrote on last edited by
                    #9

                    'void PoHavarii::Form1::textBox1_KeyDown(System::Object ^,System::Windows::Forms::KeyEventArgs ^)' : the specified function does not match the delegate type 'void (System::Object ^,System::EventArgs ^)' It needs other function, but we are on the right way :) Many thanks

                    P 1 Reply Last reply
                    0
                    • P Polar_Sheep

                      'void PoHavarii::Form1::textBox1_KeyDown(System::Object ^,System::Windows::Forms::KeyEventArgs ^)' : the specified function does not match the delegate type 'void (System::Object ^,System::EventArgs ^)' It needs other function, but we are on the right way :) Many thanks

                      P Offline
                      P Offline
                      Polar_Sheep
                      wrote on last edited by
                      #10

                      this->textBox1->KeyDown += gcnew System::Windows::Forms::KeyEventHandler(this, &Form1::textBox1_KeyDown); :-D Thank you very much and sorry for your time

                      M 1 Reply Last reply
                      0
                      • P Polar_Sheep

                        this->textBox1->KeyDown += gcnew System::Windows::Forms::KeyEventHandler(this, &Form1::textBox1_KeyDown); :-D Thank you very much and sorry for your time

                        M Offline
                        M Offline
                        Mark Salsbery
                        wrote on last edited by
                        #11

                        Oops! You got it :)

                        Mark Salsbery Microsoft MVP - Visual C++ :java:

                        1 Reply Last reply
                        0
                        • M Mark Salsbery

                          Yes you need to create the delegate and add it to the event. Can you show that code? Mark

                          Mark Salsbery Microsoft MVP - Visual C++ :java:

                          D Offline
                          D Offline
                          dybs
                          wrote on last edited by
                          #12

                          I assume you added the TextBox to your form using the designer. An easy way to add events to a control is to view the Properties window for the control (just right-click on the TextBox and select Properties if it's not already showing). The default view shows most of the public properties you can access for that control (things like Text, Enabled, Visible, Height, etc.). There's a button with a lightning bolt at the top of this window. Clicking this button will show all the events this control has. Double-clicking on an event will automatically insert the function and link it to your control in InitializeComponent(). Slightly longer explanation than I wanted, but once you walk through it, it's really easy. I just with it would place the function definitions in Form1.cpp instead of Form1.h :) - dybs

                          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