Click to See Complete Forum and Search --> : Accepting numbers in textboxes?


dannytan
March 20th, 2003, 12:52 AM
Hi all. I would like to know if it's possible to accept numbers only in textboxes? In VC++ it's possible. But i can't seem to find in the properties that accept only numbers in the properties window.
If it's not possible, then i would like to know how do i validate in the textboxes that the entered data are right format. If i were to check the textboxes by converting it to number using Int32.Parse method after a button has been clicked, it will throw an exception of invalid format. I would like to know how do i check the textboxes without throwinng an exception. Thanks

Scott MacMaster
March 20th, 2003, 03:04 AM
It is rather odd they don't have an option to do this considering how common it is to want this. Atleast I haven't found one.

Anyway, what I've done so far is add an event handler for key down. If the key isn't an allowable character then I'll change the text.

pareshgh
March 21st, 2003, 12:31 PM
you can make a control having keypress event which only accepts digits and throws rest out

methodName...(KeyPressEventArgs e)
{
if(Char.IsDigit(e.KeyChar) || e.KeyChar == '.' || e.KeyChar == 8)
{
/// blah blah blah
}
}

Paresh