Skip to content

Instantly share code, notes, and snippets.

@arslanbilal
Last active August 29, 2015 14:10
Show Gist options
  • Save arslanbilal/6377eae97157f93bc053 to your computer and use it in GitHub Desktop.
Save arslanbilal/6377eae97157f93bc053 to your computer and use it in GitHub Desktop.
Entering only numbers and just one "." character into the textBox in C#
private void textBoxKeyPressNumber(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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment