Numeric/Digits Only Input for Textbox in .NET
Friday, October 09, 2009
To set a particular textbox control to only accept Numeric/Digits values, we do not have any property for textbox .NET control. To do this, we can add add an event handler on KeyPress event - where we check the input key value on each key-press, and then only let the digit to go through.
Below is a simple example of event-handler using VC++ to accept only digits:
System::Void MyButton_KeyPress(System::Object^ sender, System::Windows::Forms::KeyPressEventArgs^ e)
{
if( !(e->KeyChar >='0' && e->KeyChar <= '9' || System::Char::IsControl( e->KeyChar )))
{
e->Handled = true;
}
}//End keyPress handler
The expression 'System::Char::IsControl( e->KeyChar )' is required because we do not want to block the control key functions like Tab/Enter etc.
In your form's class constructor, you will have to assign this event handler function using a statement like below:
Read more...
this->MyButton->KeyPress += gcnew System::Windows::Forms::KeyPressEventHandler(this, &TCPListenerForm::MyButton_KeyPress);


