Event on KeyPress
-
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:
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"; }
-
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"; }
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:
-
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:
AcceptsReturn True... Still is not work. Shall I have some code in Initialize component? I mean something like this: this->textBox1->KeyDown = gcnew... something?
-
AcceptsReturn True... Still is not work. Shall I have some code in Initialize component? I mean something like this: this->textBox1->KeyDown = gcnew... something?
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:
-
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:
#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"; } }; }
-
#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"; } }; }
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
-
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
'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
-
'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
this->textBox1->KeyDown += gcnew System::Windows::Forms::KeyEventHandler(this, &Form1::textBox1_KeyDown); :-D Thank you very much and sorry for your time
-
this->textBox1->KeyDown += gcnew System::Windows::Forms::KeyEventHandler(this, &Form1::textBox1_KeyDown); :-D Thank you very much and sorry for your time
Oops! You got it :)
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
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:
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