Last active
July 6, 2021 10:37
-
-
Save Mahno74/665bc2a01a538e4c23fb4edda76f0a1e to your computer and use it in GitHub Desktop.
Контроль допустимых символов в текстовое поле
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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