Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void DecimalInput(object sender, KeyPressEventArgs args)
- {
- const int BACKSPACE = 8;
- const int DECIMAL_POINT = 46;
- const int ZERO = 48;
- const int NINE = 57;
- const int NOT_FOUND = -1;
- int keyvalue = (int)args.KeyChar; // not really necessary to cast to int
- if ((keyvalue == BACKSPACE) ||
- ((keyvalue >= ZERO) && (keyvalue <= NINE)))
- return;
- // Allow the first (but only the first) decimal point
- if ((keyvalue == DECIMAL_POINT) &&
- ((sender as TextBox).Text.IndexOf(".") == NOT_FOUND))
- return;
- // Allow nothing else
- args.Handled = true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement