Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void txtDigitsWithDotsAndMinus_KeyPress(object sender, KeyPressEventArgs e)
- {
- TextBox txt = (TextBox)sender;
- int pos = txt.SelectionStart;
- //ввод точки (запятой)
- if ((txt.Text.StartsWith(".")) || (txt.Text.StartsWith(",")))
- {
- // добавление лидирующего ноля
- txt.Text = "0" + txt.Text;
- txt.SelectionStart = pos + 1;
- }
- if ((e.KeyChar == '.') || (e.KeyChar == ','))
- {
- if (txt.Text.Contains(".") || txt.Text.Contains(","))
- {
- e.Handled = true;
- return;
- }
- return;
- }
- //ввод минуса
- if (e.KeyChar == '-')
- {
- if (txt.Text.StartsWith("-"))
- {
- txt.Text = txt.Text.Substring(1);
- txt.SelectionStart = pos - 1;
- }
- else
- {
- txt.Text = "-" + txt.Text;
- txt.SelectionStart = pos + 1;
- }
- e.Handled = true;
- return;
- }
- //ввод цифр
- if (!(Char.IsDigit(e.KeyChar)))
- {
- if ((e.KeyChar != (char)Keys.Back))
- {
- e.Handled = true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement