[Tips] C# TextBox only accept number not character

0
20

You can use below function. This function can help you just allow input number not characters.

C# TextBox only accept number not character
// Developer: VịLH / Zidane (huuvi168@gmail.com)
// Last Modified: 2016-07-01

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
     if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && (e.KeyChar != '.'))
           e.Handled = true;

      // only allow one decimal point
      if ((e.KeyChar == '.') && ((sender as TextBox).Text.IndexOf('.') > -1))
           e.Handled = true;
}

Any feedback or question, leave your comment, we can disccuss about it!
Zidane
http://learn-tech-tips.blogspot.com/