Solution:
Method 1: Use Numericupdown controlConsider using Numericupdown control, it provides a built -in numerical input filtering function. It also easily uses keyboard arrows to increase and decreases. Method 2: Processing the keyboard incident
For more common solutions, process keyboard events to verify input. The following is an example of the code processing program on the two events on TextBox:
You can customize this code according to specific needs:
If the decimal bit is not allowed, please remove the check of '.'.
private void textBox1_KeyPress(object sender, KeyPressEventArgs e) { if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && (e.KeyChar != '-')) //允许负数 { e.Handled = true; } }
If the negative value is allowed, please add a check of '-'.
The above is the detailed content of How to Restrict a Windows Forms TextBox to Accept Only Integer Input?. For more information, please follow other related articles on the PHP Chinese website!