- If
е числа, то я пишу
in TextBoxе числа, то я пишу
textBox2.Clear (); `But all the same, the last character entered remains. For example, if I enter "Yvfpa", then there will be "a". - How do I put a limit on the number of digits entered in the textbox. Because if I exceed 32,767, the program will crash.
- 1) textBox2.Text = String.Empty The problem persists. - fiera
- Mysticism or you somewhere in the code add the last character yourself. - Murad
- pastie.org/4585142 - fiera
- And what is the meaning of NumPad? I do not seem to enter numbers from them. - fiera
|
1 answer
1) textBox2.Text = String.Empty 2) textBox2.MaxLength = int.MaxValue;
Use the NumericUpDown control for this .
private bool _number = false; private void textBox2_KeyDown_1(object sender, KeyEventArgs e) { _number = false; if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9) // вводим число if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9) // или число на цифровой клавиатуре if (e.KeyCode != Keys.Back) // нельзя вводить пробелы _number = true; label1.Text = (_number) ? "Только цифры!" : "эмпти"; if (Control.ModifierKeys == Keys.Shift) _number = true; } private void textBox2_KeyPress(object sender, KeyPressEventArgs e) { if (_number) // если не число то e.Handled = true; // отменяем ввод }
|