Last active
July 6, 2021 10:39
-
-
Save Mahno74/0c57c0f0640090f20b9bc460a915d954 to your computer and use it in GitHub Desktop.
Ограничение на вид ввода в TextBox
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 >= '0') && (e.KeyChar <= '9')) //разрешение цифр от 0 до 9 | |
return; | |
// в поле редактирование не может быть больше одной точки если вводят запятую меняем ее на точку | |
if (e.KeyChar == ',') e.KeyChar = '.'; | |
if (e.KeyChar == '.') | |
// точка не может быть первым символом | |
{ if((textBox1.Text.IndexOf('.') != -1) || (textBox1.TextLength == 0)) | |
{e.Handled = true;} | |
return;} | |
// Enter, Backspsce, Esc | |
if (Char.IsControl(e.KeyChar)) | |
{ if (e.KeyChar == (char)Keys.Enter) | |
{// действие при нажатии Enter return;} | |
if (e.KeyChar == (char)Keys.Back) //разрешение backspace | |
return; | |
} | |
e.Handled = true; // остальные символы запрещены | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment