Skip to content

Instantly share code, notes, and snippets.

@vheon
Created May 25, 2012 11:51
Show Gist options
  • Save vheon/2787574 to your computer and use it in GitHub Desktop.
Save vheon/2787574 to your computer and use it in GitHub Desktop.
private void numericOnlyTextBox_KeyUp(object sender, KeyEventArgs e)
{
TextBox textBoxControl = sender as TextBox;
string[] invalidCharacters = { "*", "#", ",", "(", ")", "x", "-", " ", "@", "." };
for (int index = 0; index < invalidCharacters.Length; ++index)
{
textBoxControl.Text = textBoxControl.Text.Replace(invalidCharacters[index], "");
}
textBoxControl.SelectionStart = textBoxControl.Text.Length;
}
private void numericOnlyTextBox_KeyDown(object sender, KeyEventArgs e)
{
TextBox textBoxControl = sender as TextBox;
int n = 2; //TODO: adjust this as the max number of digits
if (textBoxControl.Text.Length > n)
{
e.Handled = true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment