KeyPressEventHandler Delegate
.NET Framework (current version)
Namespace:
System.Windows.Forms
Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
Return to top
Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
Parameters
- sender
-
Type:
System::Object^
The source of the event.
- e
-
Type:
System.Windows.Forms::KeyPressEventArgs^
A KeyPressEventArgs that contains the event data.
Set KeyPressEventArgs::Handled to true to cancel the KeyPress event. This keeps the control from processing the key press.
When you create a KeyPressEventHandler delegate, you identify the method that will handle the event. To associate the event with your event handler, add an instance of the delegate to the event. The event handler is called whenever the event occurs, unless you remove the delegate. For more information about handling events with delegates, see Handling and Raising Events.
#using <System.dll> #using <System.Drawing.dll> #using <System.Windows.Forms.dll> using namespace System; using namespace System::Windows::Forms; public ref class Form1: public Form { public: Form1() { // Create a TextBox control. TextBox^ tb = gcnew TextBox; this->Controls->Add( tb ); tb->KeyPress += gcnew KeyPressEventHandler( this, &Form1::keypressed ); } private: void keypressed( Object^ /*o*/, KeyPressEventArgs^ e ) { // The keypressed method uses the KeyChar property to check // whether the ENTER key is pressed. // If the ENTER key is pressed, the Handled property is set to true, // to indicate the event is handled. if ( e->KeyChar == (char)13 ) e->Handled = true; } }; int main() { Application::Run( gcnew Form1 ); }
// Boolean flag used to determine when a character other than a number is entered. private: bool nonNumberEntered; // Handle the KeyDown event to determine the type of character entered into the control. void textBox1_KeyDown( Object^ /*sender*/, System::Windows::Forms::KeyEventArgs^ e ) { // Initialize the flag to false. nonNumberEntered = false; // Determine whether the keystroke is a number from the top of the keyboard. if ( e->KeyCode < Keys::D0 || e->KeyCode > Keys::D9 ) { // Determine whether the keystroke is a number from the keypad. if ( e->KeyCode < Keys::NumPad0 || e->KeyCode > Keys::NumPad9 ) { // Determine whether the keystroke is a backspace. if ( e->KeyCode != Keys::Back ) { // A non-numerical keystroke was pressed. // Set the flag to true and evaluate in KeyPress event. nonNumberEntered = true; } } } //If shift key was pressed, it's not a number. if (Control::ModifierKeys == Keys::Shift) { nonNumberEntered = true; } } // This event occurs after the KeyDown event and can be used to prevent // characters from entering the control. void textBox1_KeyPress( Object^ /*sender*/, System::Windows::Forms::KeyPressEventArgs^ e ) { // Check for the flag being set in the KeyDown event. if ( nonNumberEntered == true ) { // Stop the character from being entered into the control since it is non-numerical. e->Handled = true; } }
.NET Framework
Available since 1.1
Available since 1.1
Show: