Advertisement
Guest User

Untitled

a guest
Feb 5th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.75 KB | None | 0 0
  1. private void DecimalInput(object sender, KeyPressEventArgs args)
  2.         {
  3.             const int BACKSPACE = 8;
  4.             const int DECIMAL_POINT = 46;
  5.             const int ZERO = 48;
  6.             const int NINE = 57;
  7.             const int NOT_FOUND = -1;
  8.  
  9.             int keyvalue = (int)args.KeyChar; // not really necessary to cast to int
  10.  
  11.             if ((keyvalue == BACKSPACE) ||
  12.             ((keyvalue >= ZERO) && (keyvalue <= NINE)))
  13.                 return;
  14.             // Allow the first (but only the first) decimal point
  15.             if ((keyvalue == DECIMAL_POINT) &&
  16.             ((sender as TextBox).Text.IndexOf(".") == NOT_FOUND))
  17.                 return;
  18.             // Allow nothing else
  19.             args.Handled = true;
  20.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement