Derive a class from TextBox, override OnKeyPress, handle only numeric
input and ignore other input
for instance:
public class NumericTextBox : TextBox
{
protected override void OnKeyPress(KeyPressEventArgs e)
{
if (Char.IsDigit(e.KeyChar))
{
base.OnKeyPress (e);
}
else // just eat the event in case of non digits
{
e.Handled = true;
}
}
}
This is ofcourse 'the' way to go.... But you also have to allow for
other keystrokes as well e.g. backspace or +/- as a first character if
you want to support signed numbers.... and then one instance of decimal
point ('.') in case you want to have floating numbers.... and lets see
perhaps some other logic if you want to have exponent/matissa format...
and so on... I can only wish .NET Cf could have numeric textbox control
or maskedit for that matter... this list will also go on forever......
-Vinay.