char.IsDigit(e.KeyChar) 意思是判断你输入的是否是数字0~9,前面加“!”就是输入的是数字外的其他字符;
e.KeyChar != 8意思是判断你输入是否是退格键;
e.KeyChar != 13 是否是回车键。
1usingSystem;2usingSystem.Collections.Generic;3usingSystem.ComponentModel;4usingSystem.Data;5usingSystem.Drawing;6usingSystem.Text;7usingSystem.Windows.Forms;89namespaceTest3610{11publicpartialclassForm1 : Form12{13publicForm1()14{15InitializeComponent();16}1718PRivatevoidtextBox1_KeyPress(objectsender, KeyPressEventArgs e)19{20if((e.KeyChar !=8&& !char.IsDigit(e.KeyChar)) && e.KeyChar !=13)21{22MessageBox.Show("商品数量只能输入数字","操作提示", MessageBoxButtons.OK, MessageBoxIcon.Information);23e.Handled =true;//表示已经处理过了KeyPress事件24}25}2627privatevoidtextBox1_TextChanged(objectsender, EventArgs e)28{2930}31}
32}