I have a textbox in C# where the user can enter only decimal numbers (negative and positive).
I don't want to use MaskedText Box, I would rather implement this using the keypress event to validate inputs.
How can I achieve this?
Thanks,
*********EDIT*********++
private void mytextbox_KeyPress(object sender, KeyPressEventArgs e)
{
if (((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8 && e.KeyChar != '.' &&e.KeyChar!='-'))
{
e.Handled = true;
}
if (e.KeyChar == '.')
{
if ((sender as TextBox).Text.IndexOf(e.KeyChar) != -1)
e.Handled = true;
}
if (e.KeyChar=='-' && (sender as TextBox).SelectionStart > 0)
{
e.Handled = true;
}
}