Advertisement
tolikpunkoff

input only float numbers in TextBox

Sep 13th, 2019
861
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.53 KB | None | 0 0
  1. private void txt_KeyPress(object sender, KeyPressEventArgs e)
  2. {
  3.     //ввод только цифр с одной точкой (запятой)
  4.     if ((e.KeyChar == '.') || (e.KeyChar == ','))
  5.     {
  6.         TextBox txt = (TextBox)sender;
  7.         if (txt.Text.Contains(".") || txt.Text.Contains(","))
  8.         {
  9.             e.Handled = true;
  10.         }
  11.         return;
  12.     }
  13.  
  14.     if (!(Char.IsDigit(e.KeyChar)))
  15.     {
  16.         if ((e.KeyChar != (char)Keys.Back))
  17.         {
  18.             e.Handled = true;
  19.         }
  20.     }
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement