Skip to content

Instantly share code, notes, and snippets.

@Mahno74
Last active July 6, 2021 10:39
Show Gist options
  • Save Mahno74/0c57c0f0640090f20b9bc460a915d954 to your computer and use it in GitHub Desktop.
Save Mahno74/0c57c0f0640090f20b9bc460a915d954 to your computer and use it in GitHub Desktop.
Ограничение на вид ввода в TextBox
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