验证录入的数据必须是数字方法

王朝学院·作者佚名  2016-08-28
窄屏简体版  字體: 小  |  中  |  大  |  超大  

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}

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航