Skip to content

Instantly share code, notes, and snippets.

@Mahno74
Last active July 6, 2021 10:37
Show Gist options
  • Save Mahno74/665bc2a01a538e4c23fb4edda76f0a1e to your computer and use it in GitHub Desktop.
Save Mahno74/665bc2a01a538e4c23fb4edda76f0a1e to your computer and use it in GitHub Desktop.
Контроль допустимых символов в текстовое поле
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
//if (e.KeyChar == (char)Keys.Enter) return; //Enter
//if (e.KeyChar == (char)Keys.Back) return; //Backspace
/* Правильными символами считаются только
* цифры, точка (не более одной а все запятые заменяем точками),
* <Enter>, <Backspace>*/
if (e.KeyChar >= '0' && e.KeyChar <= '9') return; //цифра
if (Char.IsControl(e.KeyChar)) return; //<Enter>, <Backspace>, <Esc>
if (e.KeyChar == ',') e.KeyChar = '.'; //заменяем запятую точкой
if (e.KeyChar == '.')
{
if (textBox1.Text.IndexOf('.') != -1) e.Handled = true; //точка уже присутвует
return;
}
e.Handled = true; //остальное запрещено
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment